Linux / Unix sed: Delete Word From File / Input

by on September 14, 2012 · 7 comments· LAST UPDATED September 15, 2012

in

I've a file as follows:

This is a test.
One bang two three
Foo dang Bar
001 0xfg 0xA
002 0xA foo bar 0xfG
I'm done

Tutorial details
DifficultyIntermediate (rss)
Root privilegesNo
Requirementssed and bash/ksh
Estimated completion timeN/A

How do I delete all "words" from the above file which ends with a particular letter (say 'g') in each line? The output should be as follows:

This is a test.
One two three
Foo Bar
001 0xA
002 0xA foo bar
I'm done

How do I delete regex-based word using sed or awk under Linux / Unix like operating systems?

You can use any standard Unix text editing and processing utility to find and replace/delete words from the file.

Sed example

The syntax is:

 
sed 's/\<word\>//g' input
sed -e 's/\<regex-for-word\>//g' input > output
 

In this example, delete foo word from input:

 
echo 'This is a foo test' | sed -e 's/\<foo\>//g'
 

To delete all words ending with a letter 'g' in each line, enter:

 
sed -e 's/\<[a-zA-Z0-9]*[g|G]\>//g' input
 

Awk example

The syntax is:

 
awk '{gsub("word", "");print}' input
awk '{gsub("regex", "");print}' input > output
 

In this example, delete bar word from input:

 
awk '{gsub("bar", "");print}' <<< "This is a bar test"
 

To delete all words ending with a letter 'g' in each line using gnu/awk, enter:

 
awk '{gsub("[a-zA-Z0-9]*[g|G]$", "");print}' input
 

Or

 
awk '{gsub("\\<[a-zA-Z0-9]*[g|G]\\>", "");print}' input
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 7 comments… read them below or add one }

1 Chris F.A. Johnson September 15, 2012 at 3:46 am

The awk examples do not do what they are supposed to do; they delete all matching strings, not matching words.

echo 'Tough
thing
rough' | awk '{gsub("[a-zA-Z0-9]*[g|G]", "");print}'
h
h

Tough and rough do not end in ‘g’

Reply

2 nixCraft September 15, 2012 at 5:33 am
echo 'Tough
thing
rough' | awk '{gsub("[a-zA-Z0-9]*[g|G]$", "");print}'

Yes, $ missing at the end of regex. The faq has been updated. Thanks for the head up.

Reply

3 Jalal Hajigholamali September 15, 2012 at 4:55 am

Hi,
Very useful article.
Thanks a lot…

Reply

4 Logicos September 15, 2012 at 9:24 pm
sed 's/[^ ]*g//gi' input
This is a test.
One  two three
Foo  Bar
001  0xA
002 0xA foo bar
I'm done

Reply

5 nixCraft September 16, 2012 at 4:45 am

Thanks for sharing the nifty regex :)

Reply

6 Abdul Kadir October 4, 2012 at 10:24 am

Thanks a lots…….

Reply

7 Holley November 9, 2014 at 11:52 am

Interesting article, I think it’s likely to prove helpful in the long run with my work.

Reply

Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: