Skip to main content
added 146 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line.

You need to pass the shell variable into awk.

for ((i=1; i<=$count; i++))
do
    abc=$(awk -F "," -v col=$i 'NR==1 {print $col}' $file)
    #................^^^^^^^^^...............^^^^
    echo "$abc"
done

However, there's a better way than calling awk 3 separate times:

while IFS=, read -ra fields; do
    printf "%s\n" "${fields[@]}"
    # or do stuff with the individual fields
    for ((i=0; i < ${#fields[@]}; i++)); do
        echo "${fields[i]}"
    done
done < "$file" 
  • the while loop, with the file's contents redirected into it, loop over the lines of the file
  • IFS=, instructs the read command to do word splitting on commas
  • read -r means to not interpret any special characters as anything but regular chars.
  • read -a fields reads the words in the line into an array variable named fields

Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line.

You need to pass the shell variable into awk.

for ((i=1; i<=$count; i++))
do
    abc=$(awk -F "," -v col=$i 'NR==1 {print $col}' $file)
    #................^^^^^^^^^...............^^^^
    echo "$abc"
done

However, there's a better way than calling awk 3 separate times:

while IFS=, read -ra fields; do
    printf "%s\n" "${fields[@]}"
done < "$file" 
  • the while loop, with the file's contents redirected into it, loop over the lines of the file
  • IFS=, instructs the read command to do word splitting on commas
  • read -r means to not interpret any special characters as anything but regular chars.
  • read -a fields reads the words in the line into an array variable named fields

Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line.

You need to pass the shell variable into awk.

for ((i=1; i<=$count; i++))
do
    abc=$(awk -F "," -v col=$i 'NR==1 {print $col}' $file)
    #................^^^^^^^^^...............^^^^
    echo "$abc"
done

However, there's a better way than calling awk 3 separate times:

while IFS=, read -ra fields; do
    printf "%s\n" "${fields[@]}"
    # or do stuff with the individual fields
    for ((i=0; i < ${#fields[@]}; i++)); do
        echo "${fields[i]}"
    done
done < "$file" 
  • the while loop, with the file's contents redirected into it, loop over the lines of the file
  • IFS=, instructs the read command to do word splitting on commas
  • read -r means to not interpret any special characters as anything but regular chars.
  • read -a fields reads the words in the line into an array variable named fields
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line.

You need to pass the shell variable into awk.

for ((i=1; i<=$count; i++))
do
    abc=$(awk -F "," -v col=$i 'NR==1 {print $col}' $file)
    #................^^^^^^^^^...............^^^^
    echo "$abc"
done

However, there's a better way than calling awk 3 separate times:

while IFS=, read -ra fields; do
    printf "%s\n" "${fields[@]}"
done < "$file" 
  • the while loop, with the file's contents redirected into it, loop over the lines of the file
  • IFS=, instructs the read command to do word splitting on commas
  • read -r means to not interpret any special characters as anything but regular chars.
  • read -a fields reads the words in the line into an array variable named fields