Using this script:

awk '{print $0"\t"NR}' test | grep NA | awk '{print $21}'

I get the line numbers in my file, having "NA" in them


Using sed, can I delete these lines from my file in the same command through standard input, by piping after the last awk command? If not, is there any way to do it in a simple way?

  • 2
    it would be better if you have posted a testable input fragment of your test file and the final result Feb 26, 2018 at 10:24
  • yeah, please clarify if grep -v 'NA' test doesn't solve your requirement...
    – Sundeep
    Feb 26, 2018 at 10:44

2 Answers 2


The simpler way is to use only sed:

sed '/NA/d' test >test.new

If you want to do in-place editing with GNU sed (this will modify the file test):

sed -i '/NA/d' test

The sed expression /NA/d will apply the d command on all lines in the input that matches the regular expression NA. The d command deletes lines.

If the line numbers were all you had, then the following would have worked too:

some_command | sed 's/$/d/' | sed -f /dev/stdin test >output

where some_command generates the line numbers that you'd like to delete from the file test.

The first sed turns the stream of numbers into a sed script by adding a d to each line. A line reading 100d would be interpreted as "delete line 100". This is then fed to the second sed as the actual script (it's reading the script via /dev/stdin) and it is applied to the file test.

The equivalent thing in a shell that knows about process substitutions:

sed -f <( some_command | sed 's/$/d/' ) test >output

But this is sillyness if you just want to delete lines containing the string NA.


Would it be too boring to just write this as one awk command?

awk '!/NA/' test 

The default action is to print the whole line, so this is the same as !/NA/ { print $0 } and would print any line that doesn't contain NA.

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.