Linux SHELL script, read each row for different number of columns











up vote
1
down vote

favorite












I have file and for example values in it:



1 value1.1 value1.2
2 value2.1
3 value3.1 value3.2 value3.3


I need to read values using the shell script from it but number of columns in each row is different!!!
I know that if for example I want to read second column I will do it like this (for row number as input parameter)



$ awk -v key=1 '$1 == key { print $2 }' input.txt
value1.1


But as I mentioned number of columns is different for each row.
How to make this read dynamic?



For example:
if input parameter is 1 it means I should read columns from the first row so output should be



value1.1 value1.2


if input parameter is 2 it means I should read columns from the second row so output should be



value2.1


if input parameter is 3 it means I should read columns from the third row so output should be



value3.1 value3.2 value3.2


Th point is that number of columns is not static and I should read columns from that specific row until the end of the row.



Thank you










share|improve this question
























  • What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
    – Amadan
    Nov 13 at 7:05








  • 1




    @Dejan, please do mention expected sample output too in your post.
    – RavinderSingh13
    Nov 13 at 7:13










  • There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
    – tshiono
    Nov 13 at 7:14










  • @RavinderSingh13 I updated. Thank you
    – Dejan
    Nov 13 at 7:15










  • @tshiono I updated. Thank you
    – Dejan
    Nov 13 at 7:15















up vote
1
down vote

favorite












I have file and for example values in it:



1 value1.1 value1.2
2 value2.1
3 value3.1 value3.2 value3.3


I need to read values using the shell script from it but number of columns in each row is different!!!
I know that if for example I want to read second column I will do it like this (for row number as input parameter)



$ awk -v key=1 '$1 == key { print $2 }' input.txt
value1.1


But as I mentioned number of columns is different for each row.
How to make this read dynamic?



For example:
if input parameter is 1 it means I should read columns from the first row so output should be



value1.1 value1.2


if input parameter is 2 it means I should read columns from the second row so output should be



value2.1


if input parameter is 3 it means I should read columns from the third row so output should be



value3.1 value3.2 value3.2


Th point is that number of columns is not static and I should read columns from that specific row until the end of the row.



Thank you










share|improve this question
























  • What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
    – Amadan
    Nov 13 at 7:05








  • 1




    @Dejan, please do mention expected sample output too in your post.
    – RavinderSingh13
    Nov 13 at 7:13










  • There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
    – tshiono
    Nov 13 at 7:14










  • @RavinderSingh13 I updated. Thank you
    – Dejan
    Nov 13 at 7:15










  • @tshiono I updated. Thank you
    – Dejan
    Nov 13 at 7:15













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have file and for example values in it:



1 value1.1 value1.2
2 value2.1
3 value3.1 value3.2 value3.3


I need to read values using the shell script from it but number of columns in each row is different!!!
I know that if for example I want to read second column I will do it like this (for row number as input parameter)



$ awk -v key=1 '$1 == key { print $2 }' input.txt
value1.1


But as I mentioned number of columns is different for each row.
How to make this read dynamic?



For example:
if input parameter is 1 it means I should read columns from the first row so output should be



value1.1 value1.2


if input parameter is 2 it means I should read columns from the second row so output should be



value2.1


if input parameter is 3 it means I should read columns from the third row so output should be



value3.1 value3.2 value3.2


Th point is that number of columns is not static and I should read columns from that specific row until the end of the row.



Thank you










share|improve this question















I have file and for example values in it:



1 value1.1 value1.2
2 value2.1
3 value3.1 value3.2 value3.3


I need to read values using the shell script from it but number of columns in each row is different!!!
I know that if for example I want to read second column I will do it like this (for row number as input parameter)



$ awk -v key=1 '$1 == key { print $2 }' input.txt
value1.1


But as I mentioned number of columns is different for each row.
How to make this read dynamic?



For example:
if input parameter is 1 it means I should read columns from the first row so output should be



value1.1 value1.2


if input parameter is 2 it means I should read columns from the second row so output should be



value2.1


if input parameter is 3 it means I should read columns from the third row so output should be



value3.1 value3.2 value3.2


Th point is that number of columns is not static and I should read columns from that specific row until the end of the row.



Thank you







linux bash shell sh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 7:56

























asked Nov 13 at 7:02









Dejan

55382147




55382147












  • What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
    – Amadan
    Nov 13 at 7:05








  • 1




    @Dejan, please do mention expected sample output too in your post.
    – RavinderSingh13
    Nov 13 at 7:13










  • There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
    – tshiono
    Nov 13 at 7:14










  • @RavinderSingh13 I updated. Thank you
    – Dejan
    Nov 13 at 7:15










  • @tshiono I updated. Thank you
    – Dejan
    Nov 13 at 7:15


















  • What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
    – Amadan
    Nov 13 at 7:05








  • 1




    @Dejan, please do mention expected sample output too in your post.
    – RavinderSingh13
    Nov 13 at 7:13










  • There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
    – tshiono
    Nov 13 at 7:14










  • @RavinderSingh13 I updated. Thank you
    – Dejan
    Nov 13 at 7:15










  • @tshiono I updated. Thank you
    – Dejan
    Nov 13 at 7:15
















What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
– Amadan
Nov 13 at 7:05






What does "dynamic" mean specifically, to you? What do you want to happen when your "second column reader" encounters a one-column row?
– Amadan
Nov 13 at 7:05






1




1




@Dejan, please do mention expected sample output too in your post.
– RavinderSingh13
Nov 13 at 7:13




@Dejan, please do mention expected sample output too in your post.
– RavinderSingh13
Nov 13 at 7:13












There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
– tshiono
Nov 13 at 7:14




There's no problem reading varaible #columns. The point is what you want to do with them. It will be appreciated if you can illustrate with example data and expected output.
– tshiono
Nov 13 at 7:14












@RavinderSingh13 I updated. Thank you
– Dejan
Nov 13 at 7:15




@RavinderSingh13 I updated. Thank you
– Dejan
Nov 13 at 7:15












@tshiono I updated. Thank you
– Dejan
Nov 13 at 7:15




@tshiono I updated. Thank you
– Dejan
Nov 13 at 7:15












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Then you can simply say:



awk -v key=1 'NR==key' input.txt


UPDATED



If you want to process with the column data, there will be several ways.

With awk you can say something like:



awk -v key=3 'NR==key {
for (i=1; i<=NF; i++)
printf "column %d = %sn", i, $i
}' input.txt


which outputs:



column 1 = value3.1
column 2 = value3.2
column 3 = value3.2


In awk you can access each column value by $1, $2, $3 directly or by $i indirectly where variable i holds either of 1, 2, 3.



If you prefer going with bash, try something like:



line=$(awk -v key=3 'NR==key' input.txt)
set -- $line # split into columns

for ((i=1; i<=$#; i++)); do
echo column $i = ${!i}
done


which outputs the same results.

In bash the indirect access is a little bit complex and you need to say ${!i} where i is a variable name.



Hope this helps.






share|improve this answer























  • thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
    – Dejan
    Nov 13 at 7:39










  • please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
    – Dejan
    Nov 13 at 7:56






  • 1




    I updated my answer. Please take a look and if something is unclear please let me know.
    – tshiono
    Nov 13 at 8:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275518%2flinux-shell-script-read-each-row-for-different-number-of-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Then you can simply say:



awk -v key=1 'NR==key' input.txt


UPDATED



If you want to process with the column data, there will be several ways.

With awk you can say something like:



awk -v key=3 'NR==key {
for (i=1; i<=NF; i++)
printf "column %d = %sn", i, $i
}' input.txt


which outputs:



column 1 = value3.1
column 2 = value3.2
column 3 = value3.2


In awk you can access each column value by $1, $2, $3 directly or by $i indirectly where variable i holds either of 1, 2, 3.



If you prefer going with bash, try something like:



line=$(awk -v key=3 'NR==key' input.txt)
set -- $line # split into columns

for ((i=1; i<=$#; i++)); do
echo column $i = ${!i}
done


which outputs the same results.

In bash the indirect access is a little bit complex and you need to say ${!i} where i is a variable name.



Hope this helps.






share|improve this answer























  • thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
    – Dejan
    Nov 13 at 7:39










  • please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
    – Dejan
    Nov 13 at 7:56






  • 1




    I updated my answer. Please take a look and if something is unclear please let me know.
    – tshiono
    Nov 13 at 8:22















up vote
3
down vote



accepted










Then you can simply say:



awk -v key=1 'NR==key' input.txt


UPDATED



If you want to process with the column data, there will be several ways.

With awk you can say something like:



awk -v key=3 'NR==key {
for (i=1; i<=NF; i++)
printf "column %d = %sn", i, $i
}' input.txt


which outputs:



column 1 = value3.1
column 2 = value3.2
column 3 = value3.2


In awk you can access each column value by $1, $2, $3 directly or by $i indirectly where variable i holds either of 1, 2, 3.



If you prefer going with bash, try something like:



line=$(awk -v key=3 'NR==key' input.txt)
set -- $line # split into columns

for ((i=1; i<=$#; i++)); do
echo column $i = ${!i}
done


which outputs the same results.

In bash the indirect access is a little bit complex and you need to say ${!i} where i is a variable name.



Hope this helps.






share|improve this answer























  • thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
    – Dejan
    Nov 13 at 7:39










  • please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
    – Dejan
    Nov 13 at 7:56






  • 1




    I updated my answer. Please take a look and if something is unclear please let me know.
    – tshiono
    Nov 13 at 8:22













up vote
3
down vote



accepted







up vote
3
down vote



accepted






Then you can simply say:



awk -v key=1 'NR==key' input.txt


UPDATED



If you want to process with the column data, there will be several ways.

With awk you can say something like:



awk -v key=3 'NR==key {
for (i=1; i<=NF; i++)
printf "column %d = %sn", i, $i
}' input.txt


which outputs:



column 1 = value3.1
column 2 = value3.2
column 3 = value3.2


In awk you can access each column value by $1, $2, $3 directly or by $i indirectly where variable i holds either of 1, 2, 3.



If you prefer going with bash, try something like:



line=$(awk -v key=3 'NR==key' input.txt)
set -- $line # split into columns

for ((i=1; i<=$#; i++)); do
echo column $i = ${!i}
done


which outputs the same results.

In bash the indirect access is a little bit complex and you need to say ${!i} where i is a variable name.



Hope this helps.






share|improve this answer














Then you can simply say:



awk -v key=1 'NR==key' input.txt


UPDATED



If you want to process with the column data, there will be several ways.

With awk you can say something like:



awk -v key=3 'NR==key {
for (i=1; i<=NF; i++)
printf "column %d = %sn", i, $i
}' input.txt


which outputs:



column 1 = value3.1
column 2 = value3.2
column 3 = value3.2


In awk you can access each column value by $1, $2, $3 directly or by $i indirectly where variable i holds either of 1, 2, 3.



If you prefer going with bash, try something like:



line=$(awk -v key=3 'NR==key' input.txt)
set -- $line # split into columns

for ((i=1; i<=$#; i++)); do
echo column $i = ${!i}
done


which outputs the same results.

In bash the indirect access is a little bit complex and you need to say ${!i} where i is a variable name.



Hope this helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 8:59

























answered Nov 13 at 7:32









tshiono

1,428134




1,428134












  • thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
    – Dejan
    Nov 13 at 7:39










  • please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
    – Dejan
    Nov 13 at 7:56






  • 1




    I updated my answer. Please take a look and if something is unclear please let me know.
    – tshiono
    Nov 13 at 8:22


















  • thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
    – Dejan
    Nov 13 at 7:39










  • please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
    – Dejan
    Nov 13 at 7:56






  • 1




    I updated my answer. Please take a look and if something is unclear please let me know.
    – tshiono
    Nov 13 at 8:22
















thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
– Dejan
Nov 13 at 7:39




thanks. And just one small question - how can I read value by value when I have output for example for the third column value3.1 value3.2 value3.2 and now I would like to process one value by one (I need to take and parse one value by one)
– Dejan
Nov 13 at 7:39












please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
– Dejan
Nov 13 at 7:56




please if you can just add how to read value by value - one by one from this output (for example value3.1 value3.2 value3.3)
– Dejan
Nov 13 at 7:56




1




1




I updated my answer. Please take a look and if something is unclear please let me know.
– tshiono
Nov 13 at 8:22




I updated my answer. Please take a look and if something is unclear please let me know.
– tshiono
Nov 13 at 8:22


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275518%2flinux-shell-script-read-each-row-for-different-number-of-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?