Shell - Print values in a comma separated string

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a txt file with some comma separated values.
cat file.txt
abc,def,ghi
abc,ghi
def,abc,ghi
def,abc
abc,def
abc,def,ghi
I want to print these values with while do read line from file separated by comma.
Eg:
expecting output for Line no 1:
first col=abc
second col=def
third col=ghi
expecting output for Line no 2:
first col=abc
second col=ghi
If the line has three values then the read line should print
first col=value
second col=value
third col=value
else
first col=value
second col=value
How can I create this shell script?
linux shell printing echo readline
add a comment |
up vote
0
down vote
favorite
I have a txt file with some comma separated values.
cat file.txt
abc,def,ghi
abc,ghi
def,abc,ghi
def,abc
abc,def
abc,def,ghi
I want to print these values with while do read line from file separated by comma.
Eg:
expecting output for Line no 1:
first col=abc
second col=def
third col=ghi
expecting output for Line no 2:
first col=abc
second col=ghi
If the line has three values then the read line should print
first col=value
second col=value
third col=value
else
first col=value
second col=value
How can I create this shell script?
linux shell printing echo readline
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a txt file with some comma separated values.
cat file.txt
abc,def,ghi
abc,ghi
def,abc,ghi
def,abc
abc,def
abc,def,ghi
I want to print these values with while do read line from file separated by comma.
Eg:
expecting output for Line no 1:
first col=abc
second col=def
third col=ghi
expecting output for Line no 2:
first col=abc
second col=ghi
If the line has three values then the read line should print
first col=value
second col=value
third col=value
else
first col=value
second col=value
How can I create this shell script?
linux shell printing echo readline
I have a txt file with some comma separated values.
cat file.txt
abc,def,ghi
abc,ghi
def,abc,ghi
def,abc
abc,def
abc,def,ghi
I want to print these values with while do read line from file separated by comma.
Eg:
expecting output for Line no 1:
first col=abc
second col=def
third col=ghi
expecting output for Line no 2:
first col=abc
second col=ghi
If the line has three values then the read line should print
first col=value
second col=value
third col=value
else
first col=value
second col=value
How can I create this shell script?
linux shell printing echo readline
linux shell printing echo readline
edited Nov 26 at 19:03
Rui F Ribeiro
38.4k1477127
38.4k1477127
asked Nov 26 at 18:52
SQLadmin
1095
1095
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
$ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
line 1
Col 1=abc
Col 2=def
Col 3=ghi
line 2
Col 1=abc
Col 2=ghi
line 3
Col 1=def
Col 2=abc
Col 3=ghi
line 4
Col 1=def
Col 2=abc
line 5
Col 1=abc
Col 2=def
line 6
Col 1=abc
Col 2=def
Col 3=ghi
If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]
– DopeGhoti
Nov 26 at 19:05
add a comment |
up vote
1
down vote
With bash you could do
ordinals=( first second third fourth fifth sixth )
n=0
while IFS=, read -ra cols; do
echo "line $((++n))"
for i in "$!cols[@]"; do
echo "$ordinals[i] col=$cols[i]"
done
done < file
That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.
For the first 3 lines, we get
line 1
first col=abc
second col=def
third col=ghi
line 2
first col=abc
second col=ghi
line 3
first col=def
second col=abc
third col=ghi
add a comment |
up vote
0
down vote
Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:
#!/bin/sh
while IFS=, read -r first second third
do
printf 'Line %d:n' "$(( ++n ))"
$first:+printf 'First:t%sn' "$first"
$second:+printf 'Second:t%sn' "$second"
$third:+printf 'Third:t%sn' "$third"
done
The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.
Testing on the provided data:
$ ./script.sh <file
Line 1:
First: abc
Second: def
Third: ghi
Line 2:
First: abc
Second: ghi
Line 3:
First: def
Second: abc
Third: ghi
Line 4:
First: def
Second: abc
Line 5:
First: abc
Second: def
Line 6:
First: abc
Second: def
Third: ghi
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
$ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
line 1
Col 1=abc
Col 2=def
Col 3=ghi
line 2
Col 1=abc
Col 2=ghi
line 3
Col 1=def
Col 2=abc
Col 3=ghi
line 4
Col 1=def
Col 2=abc
line 5
Col 1=abc
Col 2=def
line 6
Col 1=abc
Col 2=def
Col 3=ghi
If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]
– DopeGhoti
Nov 26 at 19:05
add a comment |
up vote
0
down vote
accepted
$ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
line 1
Col 1=abc
Col 2=def
Col 3=ghi
line 2
Col 1=abc
Col 2=ghi
line 3
Col 1=def
Col 2=abc
Col 3=ghi
line 4
Col 1=def
Col 2=abc
line 5
Col 1=abc
Col 2=def
line 6
Col 1=abc
Col 2=def
Col 3=ghi
If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]
– DopeGhoti
Nov 26 at 19:05
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
$ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
line 1
Col 1=abc
Col 2=def
Col 3=ghi
line 2
Col 1=abc
Col 2=ghi
line 3
Col 1=def
Col 2=abc
Col 3=ghi
line 4
Col 1=def
Col 2=abc
line 5
Col 1=abc
Col 2=def
line 6
Col 1=abc
Col 2=def
Col 3=ghi
If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.
$ awk -F, ' print "line " NR; for (i=1;i<=NF;i++) print "Col " i "="$i ' input
line 1
Col 1=abc
Col 2=def
Col 3=ghi
line 2
Col 1=abc
Col 2=ghi
line 3
Col 1=def
Col 2=abc
Col 3=ghi
line 4
Col 1=def
Col 2=abc
line 5
Col 1=abc
Col 2=def
line 6
Col 1=abc
Col 2=def
Col 3=ghi
If you really want to transliterate from numerical columns to "first", "second", et cetera, you can define an array and use i as the index to look up the word that matches the number.
answered Nov 26 at 18:58
DopeGhoti
42.8k55181
42.8k55181
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]
– DopeGhoti
Nov 26 at 19:05
add a comment |
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]
– DopeGhoti
Nov 26 at 19:05
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
Thanks for your answer. Its awesome, If I want to use the same case instead of a file, I want to do inline, like ` awk -F, ' for (i=1;i<=NF;i++) print "Col " i "="$i ' "abc,def,ghi" ` How can I use the same command for this?
– SQLadmin
Nov 26 at 19:04
echo "abc,def,ghi" | awk [...]– DopeGhoti
Nov 26 at 19:05
echo "abc,def,ghi" | awk [...]– DopeGhoti
Nov 26 at 19:05
add a comment |
up vote
1
down vote
With bash you could do
ordinals=( first second third fourth fifth sixth )
n=0
while IFS=, read -ra cols; do
echo "line $((++n))"
for i in "$!cols[@]"; do
echo "$ordinals[i] col=$cols[i]"
done
done < file
That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.
For the first 3 lines, we get
line 1
first col=abc
second col=def
third col=ghi
line 2
first col=abc
second col=ghi
line 3
first col=def
second col=abc
third col=ghi
add a comment |
up vote
1
down vote
With bash you could do
ordinals=( first second third fourth fifth sixth )
n=0
while IFS=, read -ra cols; do
echo "line $((++n))"
for i in "$!cols[@]"; do
echo "$ordinals[i] col=$cols[i]"
done
done < file
That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.
For the first 3 lines, we get
line 1
first col=abc
second col=def
third col=ghi
line 2
first col=abc
second col=ghi
line 3
first col=def
second col=abc
third col=ghi
add a comment |
up vote
1
down vote
up vote
1
down vote
With bash you could do
ordinals=( first second third fourth fifth sixth )
n=0
while IFS=, read -ra cols; do
echo "line $((++n))"
for i in "$!cols[@]"; do
echo "$ordinals[i] col=$cols[i]"
done
done < file
That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.
For the first 3 lines, we get
line 1
first col=abc
second col=def
third col=ghi
line 2
first col=abc
second col=ghi
line 3
first col=def
second col=abc
third col=ghi
With bash you could do
ordinals=( first second third fourth fifth sixth )
n=0
while IFS=, read -ra cols; do
echo "line $((++n))"
for i in "$!cols[@]"; do
echo "$ordinals[i] col=$cols[i]"
done
done < file
That reads the words in each line into an array named cols, then we interate over the indices of that array so we can correlate the value to the ordinal.
For the first 3 lines, we get
line 1
first col=abc
second col=def
third col=ghi
line 2
first col=abc
second col=ghi
line 3
first col=def
second col=abc
third col=ghi
edited Nov 26 at 19:28
answered Nov 26 at 19:22
glenn jackman
49.6k569106
49.6k569106
add a comment |
add a comment |
up vote
0
down vote
Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:
#!/bin/sh
while IFS=, read -r first second third
do
printf 'Line %d:n' "$(( ++n ))"
$first:+printf 'First:t%sn' "$first"
$second:+printf 'Second:t%sn' "$second"
$third:+printf 'Third:t%sn' "$third"
done
The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.
Testing on the provided data:
$ ./script.sh <file
Line 1:
First: abc
Second: def
Third: ghi
Line 2:
First: abc
Second: ghi
Line 3:
First: def
Second: abc
Third: ghi
Line 4:
First: def
Second: abc
Line 5:
First: abc
Second: def
Line 6:
First: abc
Second: def
Third: ghi
add a comment |
up vote
0
down vote
Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:
#!/bin/sh
while IFS=, read -r first second third
do
printf 'Line %d:n' "$(( ++n ))"
$first:+printf 'First:t%sn' "$first"
$second:+printf 'Second:t%sn' "$second"
$third:+printf 'Third:t%sn' "$third"
done
The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.
Testing on the provided data:
$ ./script.sh <file
Line 1:
First: abc
Second: def
Third: ghi
Line 2:
First: abc
Second: ghi
Line 3:
First: def
Second: abc
Third: ghi
Line 4:
First: def
Second: abc
Line 5:
First: abc
Second: def
Line 6:
First: abc
Second: def
Third: ghi
add a comment |
up vote
0
down vote
up vote
0
down vote
Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:
#!/bin/sh
while IFS=, read -r first second third
do
printf 'Line %d:n' "$(( ++n ))"
$first:+printf 'First:t%sn' "$first"
$second:+printf 'Second:t%sn' "$second"
$third:+printf 'Third:t%sn' "$third"
done
The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.
Testing on the provided data:
$ ./script.sh <file
Line 1:
First: abc
Second: def
Third: ghi
Line 2:
First: abc
Second: ghi
Line 3:
First: def
Second: abc
Third: ghi
Line 4:
First: def
Second: abc
Line 5:
First: abc
Second: def
Line 6:
First: abc
Second: def
Third: ghi
Assuming that the input file only has at most three columns, the following uses a while-read loop to read the comma-separated values from standard input and outputs them in a format similar to what you showed:
#!/bin/sh
while IFS=, read -r first second third
do
printf 'Line %d:n' "$(( ++n ))"
$first:+printf 'First:t%sn' "$first"
$second:+printf 'Second:t%sn' "$second"
$third:+printf 'Third:t%sn' "$third"
done
The parameter expansion $variable:+word expands to word if variable is set and non-empty. The code uses this to execute printf for output if the corresponding variable contains data to be printed.
Testing on the provided data:
$ ./script.sh <file
Line 1:
First: abc
Second: def
Third: ghi
Line 2:
First: abc
Second: ghi
Line 3:
First: def
Second: abc
Third: ghi
Line 4:
First: def
Second: abc
Line 5:
First: abc
Second: def
Line 6:
First: abc
Second: def
Third: ghi
answered Nov 26 at 19:24
Kusalananda
118k16223364
118k16223364
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2funix.stackexchange.com%2fquestions%2f484264%2fshell-print-values-in-a-comma-separated-string%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