delete a column with awk or sed

SedAwk

Sed Problem Overview


I have a file with three columns. I would like to delete the 3rd column(in-place editing). How can I do this with awk or sed?

123   abc  22.3
453   abg  56.7
1236  hjg  2.3

Desired output

123  abc
453  abg
1236 hjg 

Sed Solutions


Solution 1 - Sed

try this short thing:

awk '!($3="")' file

Solution 2 - Sed

With GNU awk for inplace editing, \s/\S, and gensub() to delete

  1. the FIRST field:

    awk -i inplace '{sub(/^\S+\s*/,"")}1' file

    or

    awk -i inplace '{$0=gensub(/^\S+\s*/,"",1)}1' file

  2. the LAST field:

    awk -i inplace '{sub(/\s*\S+$/,"")}1' file

    or

    awk -i inplace '{$0=gensub(/\s*\S+$/,"",1)}1' file

  3. the Nth field where N=3:

    awk -i inplace '{$0=gensub(/\s*\S+/,"",3)}1' file

Without GNU awk you need a match()+substr() combo or multiple sub()s + vars to remove a middle field. See also Print all but the first three columns.

Solution 3 - Sed

This might work for you (GNU sed):

sed -i -r 's/\S+//3' file

If you want to delete the white space before the 3rd field:

sed -i -r 's/(\s+)?\S+//3' file

Solution 4 - Sed

It seems you could simply go with

awk '{print $1 " " $2}' file

This prints the two first fields of each line in your input file, separated with a space.

Solution 5 - Sed

Try using cut... its fast and easy

First you have repeated spaces, you can squeeze those down to a single space between columns if thats what you want with tr -s ' '

If each column already has just one delimiter between it, you can use cut -d ' ' -f-2 to print fields (columns) <= 2.

for example if your data is in a file input.txt you can do one of the following:

cat input.txt | tr -s ' ' | cut -d ' ' -f-2

Or if you better reason about this problem by removing the 3rd column you can write the following

cat input.txt | tr -s ' ' | cut -d ' ' --complement -f3

cut is pretty powerful, you can also extract ranges of bytes, or characters, in addition to columns

excerpt from the man page on the syntax of how to specify the list range

Each LIST is made up of one range, or many ranges separated by commas.
Selected input is written in the same order that it is read, and is
written exactly once. Each range is one of:

  N     N'th byte, character or field, counted from 1
  N-    from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M    from first to M'th (included) byte, character or field

so you also could have said you want specific columns 1 and 2 with...

cat input.txt | tr -s ' ' | cut -d ' ' -f1,2

Solution 6 - Sed

Try this :

awk '$3="";1' file.txt > new_file && mv new_file file.txt

or

awk '{$3="";print}' file.txt > new_file && mv new_file file.txt

Solution 7 - Sed

If you're open to a Perl solution...

perl -ane 'print "$F[0] $F[1]\n"' file

These command-line options are used:

  • -n loop around every line of the input file, do not automatically print every line

  • -a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace

  • -e execute the following perl code

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2160995View Question on Stackoverflow
Solution 1 - SedKentView Answer on Stackoverflow
Solution 2 - SedEd MortonView Answer on Stackoverflow
Solution 3 - SedpotongView Answer on Stackoverflow
Solution 4 - SedtonioView Answer on Stackoverflow
Solution 5 - SedJonView Answer on Stackoverflow
Solution 6 - SedGilles QuenotView Answer on Stackoverflow
Solution 7 - SedChris KoknatView Answer on Stackoverflow