Deleting lines from a file through stdin piping in bash

5,194

Solution 1

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.

Solution 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.

Share:
5,194
rishi
Author by

rishi

Updated on September 18, 2022

Comments

  • rishi
    rishi almost 2 years

    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

    326
    399
    672
    1512
    1734
    1737
    2212
    

    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?

    • RomanPerekhrest
      RomanPerekhrest over 6 years
      it would be better if you have posted a testable input fragment of your test file and the final result
    • Sundeep
      Sundeep over 6 years
      yeah, please clarify if grep -v 'NA' test doesn't solve your requirement...