Cannot select a dividing line with a two-column separator

I am trying to do the following:

  • Read the file line by line.
  • Each row has the following structure: field1;field2;field3
  • Use awk to separate each of these fields, and then process each of these fields further.

The code snippet I have is:

 while read l do n=`echo ${l} | awk --field-separator=";" '{print NF}'` field1=`echo ${l} | awk --field-separator=";" '{print $1}'` field2=`echo ${l} | awk --field-separator=";" '{print $2}'` field3=`echo ${l} | awk --field-separator=";" '{print $3}'` echo ${n} ${field1} ${field2} ${field3} done < temp 

If temp contains only the following line:

 xx;yy;zz 

The answer I get on the command line is:

 1 xx;yy;zz 

I am not sure I understand this conclusion. Any explanation would be nice, given that it works for other files. I am working on a Mac while this code uses awk inside a bash script.

+4
source share
3 answers

Your awk does not know what --field-separator=";" when you do this:

 awk --field-separator=";" '{print $1}' 

your awk still uses FS by default space, and so $ 1 contains the entire input line, and $ 2 and $ 3 are empty. Use -F';' to install FS.

You are the WAY, WAY from the label in how to write the script you want. If you tell us more about what โ€œprocess each fieldโ€ is, we can help you.

+3
source

Why awk when you can do this in pure bash?

 while IFS=';' read -r field1 field2 field3; do echo "Field1: $field1" echo "Field2: $field2" echo "Field3: $field3" done < file.txt 

Or, if you do not know the number of fields:

 while IFS=';' read -ra fields; do echo "Number of fields: ${#fields[@]}" echo "Field1 ${fields[0]}" done < file.txt 
+7
source

This is probably a bug with your awk. Try other formats:

 while read l do n=`echo "${l}" | awk -F\; '{print NF}'` field1=`echo "${l}" | awk -F\; '{print $1}'` field2=`echo "${l}" | awk -F\; '{print $2}'` field3=`echo "${l}" | awk -F\; '{print $3}'` echo "${n} ${field1} ${field2} ${field3}" done < temp 

or

 while read l do n=`echo "${l}" | awk -v 'FS=;' '{print NF}'` field1=`echo "${l}" | awk -v 'FS=;' '{print $1}'` field2=`echo "${l}" | awk -v 'FS=;' '{print $2}'` field3=`echo "${l}" | awk -v 'FS=;' '{print $3}'` echo "${n} ${field1} ${field2} ${field3}" done < temp 

or

 while read l do n=`echo "${l}" | awk 'BEGIN{FS=";"}{print NF}'` field1=`echo "${l}" | awk 'BEGIN{FS=";"}{print $1}'` field2=`echo "${l}" | awk 'BEGIN{FS=";"}{print $2}'` field3=`echo "${l}" | awk 'BEGIN{FS=";"}{print $3}'` echo "${n} ${field1} ${field2} ${field3}" done < temp 

Try other awks like mawk or nawk .

+2
source

Source: https://habr.com/ru/post/1500708/


All Articles