0

I have a file containing 8 columns, One of my tasks is to get the values of Column 3 and if the value of the row is not equal to 123 the 8 coloumns will be printed and hence If column 3 is equal to 123 nothing will be printed

part of the program is ->

testing=`echo $C3  | awk '!/123/ {print $C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8,}'>>final.txt` 

and the related error is awk: illegal field $(), name "C1" input record number 1, file source line number 1

where I found that for Lines not containing 123: it can be scripted as awk '!/123/' from https://stackoverflow.com/questions/3548453/negative-matching-using-grep-match-lines-that-do-not-contain-foo

I also tried

testing=`echo $C3  | awk '!/123/ ; END {print $C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8,}'>>final.txt` 

however it also gives an error.

thanks in advance

  • 2
    not quite sure what you are trying to achieve, but fields in awk are numbered $1 up and don't relate to the fields in the shell. So the value of $C3 in the shell is passed over to awk as a line and then it is broken up (space-delimited) into fields $1 $3 $4 up. Also you are trying to set variable testing while piping the output into a file? – gogoud Dec 5 '15 at 9:14
  • 1
    1) try man awk 2) use $1 , $5 to refer to value of 1st or 5th column, not $C1 or $C5. use $3 != 123 (before { } part ) to filter on third column – Archemar Dec 5 '15 at 9:16
  • I have another idea however i am finding it difficult how to implement it, If i have a value in column 3 equal to 123 then delete the entire row corresponding to that entry. so far i ve done this echo $C3 | awk '$1==123 {print $1}' I just need help in how to implement the deleting procedure in awk Thanks in advance. – user146120 Dec 5 '15 at 12:07
  • Please edit your question and show us your input. What is $C3? What are the other $CN supposed to be? – terdon Dec 5 '15 at 12:09
  • $C3 refer to column 3 in a particular file – user146120 Dec 5 '15 at 12:37
1

awk is specifically designed to deal with columnar files. It will automatically split each input line on whitespace (or anything else you give it via its -F switch) and the columns will then be available to awk as $1, $2, ..., $N.

So, to print lines whose 3rd column (as defined by whitespace) is not 123, you would do:

awk '$3!="123"' file

The default action of awk when something evaluates to true is to print. Therefore, the command above will print all lines of file whose 3rd column isn't 123.

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.