简体   繁体   中英

Linux: Columns values check in a file

I have a file with large number of columns. As a sanity check, I want to see if any of the columns have value of -1 or not.

One naive way using awk is

cat file.txt | awk '{if($column_no==-1)print}' 

This I need to repeat for each column. Is there a better way?

I might be missing something, but isn't this a job for grep? grep ' -1 ' file.txt will return all lines in whcih any colum is -1, assuming the file is space delimited.

That's not as clear as it could be, so again in monospace:

grep ' -1 ' file.txt

The space before and after the -1 are important.

EDIT:

OK, I was missing something. If knowing the column number is important then a simple for loop should do the trick:

cat file.txt|awk '{for (i=1;i<=NF;i++)if($i==-1) print i}'

NF is a built-in awk variable containing the number of fields in the record.

SUPEREDIT: Taking the comments into account we have:

awk '{for (i=1;i<=NF;i++)if($i==-1) print NR, i}' file.txt

which spits out row, column pairs. Add your own gloss as required.

awk '{ for (i=1;i<=NF;i++) {
           if ( $i == -1 ) 
               { a[$i]++ } 
           } 
     } 
     END { for (key in a) {
               print key ":" a[key]
           }
     }' INPUT_FILE

Will show the column numbers with the number of occurrences. A little modification and you can add line numbers to it (eg with associative double indexed array, using NR ...)

HTH

Something like this could work -

awk '{
for (i=1;i<=NF;i++) 
if ($i=="-1") print "-1 is present on Column: " i " on Line: "NR}' file

Test:

[jaypal:~/Temp] cat file
44 -1 23234 232 -1 -1 44 555 -1
-1 33 23234 -1 1 10 -1 555 -1

[jaypal:~/Temp] awk '{
for (i=1;i<=NF;i++) 
if ($i=="-1") print "-1 is present on Column: " i " on Line: "NR}' file
-1 is present on Column: 2 on Line: 1
-1 is present on Column: 5 on Line: 1
-1 is present on Column: 6 on Line: 1
-1 is present on Column: 9 on Line: 1
-1 is present on Column: 1 on Line: 2
-1 is present on Column: 4 on Line: 2
-1 is present on Column: 7 on Line: 2
-1 is present on Column: 9 on Line: 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM