Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

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

grep 
    -v invert match
    -E .\+ match any 1+ character
    -n output line numbers
share|improve this answer
    
I actually ended up using '^$', but the -n was the missing piece for me! Thx –  David Oneill Jun 15 '11 at 14:54
1  
Or you could just search for not-.. –  Gilles Jun 15 '11 at 17:26
    
This method prints an extra colon : after each line number... using: GNU grep 2.5.4 –  Peter.O Jun 16 '11 at 9:53

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}'
share|improve this answer
    
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. –  David Oneill Jun 15 '11 at 19:24
1  
@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 '11 at 10:18

^ 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 .
share|improve this answer
1  
You read my question wrong: I want the line numbers, not the count. But +1 for '^$' –  David Oneill Jun 15 '11 at 14:53
    
@David Yeah, I actually fixed it before you commented; I reread it and realized :). guido got it right first –  Michael Mrozek Jun 15 '11 at 14:54
    
(above comment was from when he answer contained -c instead of -n) –  David Oneill Jun 15 '11 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.