Linux / Unix sed: Delete Word From File / Input

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

ADVERTISEMENTS

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
šŸ§ Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
12 comments… add one
  • Chris F.A. Johnson Sep 15, 2012 @ 3:46

    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’

    • šŸ§ nixCraft Sep 15, 2012 @ 5:33
      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.

  • Jalal Hajigholamali Sep 15, 2012 @ 4:55

    Hi,
    Very useful article.
    Thanks a lot…

  • Logicos Sep 15, 2012 @ 21:24
    sed 's/[^ ]*g//gi' input
    
    This is a test.
    One  two three
    Foo  Bar
    001  0xA
    002 0xA foo bar 
    I'm done
    
    • šŸ§ nixCraft Sep 16, 2012 @ 4:45

      Thanks for sharing the nifty regex :)

  • Abdul Kadir Oct 4, 2012 @ 10:24

    Thanks a lots…….

  • Holley Nov 9, 2014 @ 11:52

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

  • w4rb0y Feb 7, 2016 @ 9:39

    #cat test.txt
    One two three
    Foo Bar
    001 0xA
    002 0xA foo bar
    I’m done

    #cat test.txt | sed ‘s/[^[:blank:]]*[gG][^[:blank:]]*//g’

    One two three
    Foo Bar
    001 0xA
    002 0xA foo bar
    I’m done

    • w4rb0y Feb 7, 2016 @ 17:02

      ****** Correction to my above mistake******

      #cat test.txt

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

      #cat test.txt | sed ā€˜s/[^[:blank:]]*[gG][^[:blank:]]*//gā€™

      One two three
      Foo Bar
      001 0xA
      002 0xA foo bar
      Iā€™m done

  • DodoGTA Feb 27, 2016 @ 16:18

    Thanks! (I needed to add in-place editing for my case in sed example, however)

  • khemlal Jun 13, 2017 @ 3:18

    Hello…i wanaa remove from k__ to c__ from all line in my file…for example as it below

    k__Bacteria;p__Acidobacteria;c__Chloracidobacteria 0.0 0.0 0.054 0.52
    k__Bacteria;p__Acidobacteria;c__PAUC37f 0.0 0.0 0.0 0.0
    ……………..
    and after removal it suppose to be like below
    Chloracidobacteria 0.0 0.0 0.054 0.52
    PAUC37f 0.0 0.0 0.0 0.0
    ………
    please can someone help me how to do this..
    Thanks in advance
    Khem
    …….

    • šŸ§ Vivek Gite Jun 13, 2017 @ 8:40

      Try:
      sed 's/k__.*c__//' input > output
      ## OR ##
      sed -i 's/k__.*c__//' input

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.