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
linux bash shell sh
add a comment |
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
linux bash shell sh
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
add a comment |
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
linux bash shell sh
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
linux bash shell sh
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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