How can I delete the 5th word of every line in a file?

I want to delete the 5th word of each line in a file.

The current content of the file:

File is not updated or and will be removed  
System will shut down f within 10 seconds  
Please save your work 55 or copy to other location  
Kindly cooperate with us D  

Expected output:

File is not updated and will be removed  
System will shut down within 10 seconds  
Please save your work or copy to other location  
Kindly cooperate with us
Asked By: Nainita

||

With POSIX sed:

sed -e 's/[^[:alnum:]_][[:alnum:]_][[:alnum:]_]*//4' <file
Answered By: cuonglm

A solution with cut:

cut -d ' ' -f1-4 -f6- FILE
Answered By: fd0

How about cut :

$ cut -d' ' -f1-4,6- file.txt 
File is not updated and will be removed  
System will shut down within 10 seconds  
Please save your work or copy to other location  
Kindly cooperate with us
  • -d' ' sets the delimiter as space

  • -f1-4,6- selects the first to 4th field (word), leaving the 5th one and then continue printing from 6th to the rest.

Answered By: heemayl

awk: remove the 5th field

awk '{for (i=5; i<NF; i++) $i = $(i+1); NF--};1' file

If you want to save the file in-place: https://stackoverflow.com/q/16529716/7552

You could just erase the contents of the 5th field, but that leaves 2 consecutive output field separators:

awk '{$5 = ""};1' file
Answered By: glenn jackman

Perl.

perl -ne 'print $_ =~ /^(w+ +w+ +w+ +w+ +)w+ (.*)/,"n"' file
Answered By: steve

glenn offered a solution that is equivalent to

awk '{$5=""; print}' file

As he and others have pointed out, this

  1. strips leading and trailing whitespace from every line,
  2. compresses each string of whitespace (spaces and/or tabs)
    into a single space, and
  3. leaves two spaces between the fourth and six words.

A hack to fix the third problem is

awk '{$5=""; print}' file | sed 's/  / /'

This will still leave one or more added space(s)
at the end of any line that had five or fewer words going in. 
If you can identify a word that will never appear in the input,

awk '{$5="unicorn"; print}' file | sed 's/ *unicorn//'

will handle even that (but it still leaves problems 1 and 2).

 sed 's/^(([[:blank:]]*[^[:blank:]]{1,}){4})[[:blank:]]*[^[:blank:]]*/1/' YourFile > Output.txt
  • posix sed based on space/tab separator (meta class [:blank:]])
  • keep the following space after 5th word but removing the one before

A more robust (sed take the longest pattern possible and pattern with * could miss separation or word in first version) but a bit longer version

sed 's/^([[:blank:]]*([^[:blank:]]{1,}[[:blank:]]{1,}){4}[^[:blank:]]{1,}/1/' YourFile > Output.txt
Answered By: NeronLeVelu

Using Perl > 5.10 (and successfully outputting all lines :0) ):-

perl -nE '/^((w+ +){4})w+ *(.*)/; say $1.$3' file
Answered By: Medlock Perlman

Another possibility, assuming GNU cut:

cut -d' ' -f5 --complement file.txt
Answered By: Digital Trauma
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.