How to remove a last field from a file

Hi
I have file like this :

abc def ghi 123
fgh der 456
sss ttt uuu vvv kkk 098

I need to remove the last field and the output should be like this :

abc def ghi
fgh der
sss ttt uuu vvv kkk

Thx in Advance

Use this,

sed -r "s/(^.+) .+$/\1/g" file > outputfile

We can also achieve this by following way.

rev <inputfile> | cut -d ' ' -f 2- | rev > outputfile

This should work too:
[FONT=monospace]

awk '/./' file.txt |  awk '{ field=$NF }; END{ print field}'

Here's a Perl solution:

$ cat asdf.txt
abc def ghi 123
fgh der 456
sss ttt uuu vvv kkk 098
$ perl -i -ple '@a=split /\s/; $_=join " ",@a[0..($#a-1)];' asdf.txt
$ cat asdf.txt
abc def ghi
fgh der
sss ttt uuu vvv kkk

---------- Post updated at 08:53 ---------- Previous update was at 08:51 ----------

@jostber: what exactly does this accomplish other than printing the last field of the last line?

awk '{$NF=""}1' urfile

You can use the following sed command

sed -re 's/([ 0-9A-Za-z]){3}$//g' field_file

Another one:

sed 's/\(.*\) .*/\1/' file

rdcwayx' awk solution is best suited here, as the correct sed alternative is more complicated:

sed 's/[[:space:]]*[^[:space:]]*$//' infile

or

sed 's/\(.*\)[[:space:]].*/\1/' infile

Sorry, a bit quick with my answer. Here is another awk solution:

awk 'field=$NF{ gsub(field, ""); print }' file.txt

Hello thillai,how you're ensuring that there are only three characters in the last field.Don't simply give solution for the example data given.Give some useful solutions which will work for all the situations.Else,you mention it clearly.Because,this forum isn't only helping for original poster.Others can also learn from it.

HOW CAN YOU SAY THAT I AM SIMPLY GIVING SOLUTIONS FOR THE EXAMPLE DATA GIVEN?

Don't use your internal thoughts in this forum. This is not the right place to debate about your thoughts.
Kindly Use this forum for posting technical related stuffs.

What?I am not saying my internal thoughts.I just specified that your solution won't work for all the conditions.But,you're expressing some other informations.First refer your post and tell.Why I specified means,If I specified you could change your answer.But,without changing your answer,you're doing something.

Ok Vivek! Then how could you say that I am giving solution for sample data? You could mention like "this is not working for this condition so and so...." Thats why I have mentioned about internal thoughts. And presently I am working on solving that issue only.

Hai! U can achieve this using Perl in the following way.

use strict;
use warnings;

open FH,"<file" or die "Can't open file: $!";
while ( <FH> )
{
    if (s/(\w+)$//g) #matching the last field
    {
        open RFH,">>result_file"; #opening the result file in append mode
        print RFH $`."\n"; #matching all the strings before the pattern. So this will exclude the last field
    }
}

Now the output_file will contain the following contents

abc def ghi
fgh der
sss ttt uuu vvv kkk

What about:

awk '{sub(FS$NF,F)}1' infile

This will remove all chars after the last space.

sed 's/ [^ ]\+$//' nfile

Could you explain how internally it works and i am surprised it's giving same result if replace the number 1 with any number.

danmero your code can be shorten to below with same result:-

nawk 'sub(FS$NF,"")' infile.txt

;);):wink:

---------- Post updated at 10:24 ---------- Previous update was at 10:09 ----------

more simple code in perl.

perl -wlane 'print "@F[0..$#F-1]" ;' infile.txt > outfile.txt

You can make it shorter.

perl -lane '{$,=" "; pop(@F); print  @F;}' file