Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 5 down vote accepted
# cut -d \| -f 6 test.txt | grep -v -E .\+ -n

    -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
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
@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
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


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.