1

I need to remove lines from a file that contain > 1 word. Here is an example of the file's content:

This
line is
not
what I need
printed-now.

would print

This
not
printed-now.

I have tried:

sed -n '/[a-zA-Z]* *[a-zA-Z]/p'

This is a learning activity that has me stumped.

2
  • Sounds like a school assignment, what have you attempted? – guiverc Oct 10 '19 at 9:34
  • sed -n '/[a-zA-Z]* *[a-zA-Z]/p' learning activity that has me stumped – Matt Oct 10 '19 at 9:36
3

With sed you can use this:

sed -i '/\w .*/d' file

or this, if the first word can contain anything but whitespace:

sed -i '/\S .*/d' file
  • -i: modify the file in place.
  • /\w .*/d: if the pattern /\w .*/ (i.e. a word, a space and everything after that) is matched, delete (d at the end) the whole line.
  • /\S .*/d: if the pattern /\S .*/ (i.e. anything but whitespace, a space and everything after that) is matched, delete (d at the end) the whole line.

You have many options to do the same thing with awk. In all of the following cases, -i inplace is used to modify the file in place:

  1. Print only the lines which don't have a second field:

    awk -i inplace '!$2' file
    
  2. Print only the lines for which the second field is empty:

    awk -i inplace '$2 == ""' file
    
  3. Print only the lines for which the number of fields (NF) is smaller than 2 (equal to 1) (thanks steeldriver!):

    awk -i inplace 'NF<2' file
    

    or

    awk -i inplace 'NF==1' file
    
  4. Print only the lines for which the length of the second field is not zero:

    awk -i inplace '!length($2)' file
    

Note that the -i inplace flag works only for awk versions greater than 4.1. For versions lower than this, the equivalent is to first save to an intermediate file and then rename this file as the initial one. For example, the first option would be like this:

awk '!$2' file > tmp && mv tmp file

In all cases the output is this:

This
not
printed-now.
4
  • 1
    For awk (which is more appropriate for this task than sed imho) you could simply use the value of NF i.e. awk 'NF<2' file or (to exclude empty lines as well) awk 'NF==1' file – steeldriver Oct 10 '19 at 12:17
  • @steeldriver Thanks! awk '!length($2)' file works too. Or, even simpler, awk '!$2' file. – BeastOfCaerbannog Oct 10 '19 at 12:22
  • @steeldriver Do you mind if I add your suggestion to the answer? – BeastOfCaerbannog Oct 10 '19 at 12:26
  • ... not at all ;) – steeldriver Oct 10 '19 at 12:28
1

I would use:

sed -E '/\S\s+\S/d' <Data.txt

(ie, lines that contain something which is not a space, followed by any number of spaces, followed by something which is not a space).

One
Item
Per line
This disappears
This also disappears
This-remains
  Leading-space
Trailing-spaces   

becomes:

One
Item
This-remains
  Leading-space
Trailing-spaces   
0

Use

sed -i '/what I need/c\ ' filename

Basically it follows the approach-

sed -i '/pattern/c\ line_you_want_to_insert' filename    

Here line_you_want_to_insert will be blank as you want it removed.

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.