1

I have a large pipe-delimited file where I need to find the line number of all lines where a certain field is empty.

I can use cut -d \| -f 6 filename.txt to output just that column.

What is a utility/tool/command I can use to find what output lines from the above are empty?

1

3 Answers 3

5
# cut -d \| -f 6 test.txt | grep -v -E .\+ -n

grep 
    -v invert match
    -E .\+ match any 1+ character
    -n output line numbers
4
  • I actually ended up using '^$', but the -n was the missing piece for me! Thx Jun 15, 2011 at 14:54
  • 1
    Or you could just search for not-.. Jun 15, 2011 at 17:26
  • This method prints an extra colon : after each line number... using: GNU grep 2.5.4
    – Peter.O
    Jun 16, 2011 at 9:53
  • @Peter.O then use the colon as a delimiter for a cut, i.e., cut -d \| -f 6 test.txt | grep -v -E .\+ -n | cut -d ':' -f1
    – Novice C
    Sep 27, 2016 at 20:47
5

You can combine cut and grep as others have shown, or you can use the all-purpose text filter awk.

awk -F'|' '$6 == "" {print NR}'
2
  • Nifty. Note for others that might come along: as written, this command takes input from stdin. add < filename.txt to the end to read from the file. Jun 15, 2011 at 19:24
  • 3
    @Davie Oneill: awk doesn't require the file to be via a stdin redirecton < (which can also be before the awk command)... awk can natively accept input via filename(s) as its last parameter(s).
    – Peter.O
    Jun 16, 2011 at 10:18
1

^ in a regular expression matches the beginning of the line, and $ the end, so ^$ matches empty lines. grep takes a -n argument that outputs the line numbers of matching lines instead of the lines themselves, so:

$ grep -n '^$'

You can also use -v to invert the match and count non-matching lines, in which case you want to match lines that have at least one character (where . matches any character):

$ grep -n -v .
3
  • 1
    You read my question wrong: I want the line numbers, not the count. But +1 for '^$' Jun 15, 2011 at 14:53
  • @David Yeah, I actually fixed it before you commented; I reread it and realized :). guido got it right first Jun 15, 2011 at 14:54
  • (above comment was from when he answer contained -c instead of -n) Jun 15, 2011 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .