0

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?

0
$ 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? – Bhuvanesh Nov 26 '18 at 19:04
  • echo "abc,def,ghi" | awk [...] – DopeGhoti Nov 26 '18 at 19:05
1

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
0

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%s\n' "$first"}
    ${second:+printf 'Second:\t%s\n' "$second"}
    ${third:+printf 'Third:\t%s\n' "$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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.