How to remove a last field from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove a last field from a file
# 15  
Old 03-07-2010
Hai! U can achieve this using Perl in the following way.
Code:
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
Code:
abc def ghi
fgh der
sss ttt uuu vvv kkk

# 16  
Old 03-07-2010
What about:
Code:
awk '{sub(FS$NF,F)}1' infile

# 17  
Old 03-08-2010
This will remove all chars after the last space.
Code:
sed 's/ [^ ]\+$//' nfile

# 18  
Old 03-08-2010
Quote:
Originally Posted by rdcwayx
Code:
awk '{$NF=""}1' urfile

Could you explain how internally it works and i am surprised it's giving same result if replace the number 1 with any number.
# 19  
Old 03-08-2010
Quote:
Originally Posted by danmero
What about:
Code:
awk '{sub(FS$NF,F)}1' infile

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

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

SmilieSmilieSmilie

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

Quote:
Originally Posted by thillai_selvan
Hai! U can achieve this using Perl in the following way.
Code:
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
Code:
abc def ghi
fgh der
sss ttt uuu vvv kkk

more simple code in perl.

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

# 20  
Old 03-08-2010
Quote:
Originally Posted by thillai_selvan
Hai! U can achieve this using Perl in the following way.
Code:
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
Code:
abc def ghi
fgh der
sss ttt uuu vvv kkk

You can make it shorter.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

How to remove empty field in a text file?

Hi all, I want to remove empty field in a text file. I tried to used sed. But it failed. Input: LG10_PM_map_19_LEnd 1000560 G AG AG LG10_PM_map_19_LEnd 1005621 G AG LG10_PM_map_19_LEnd 1011214 A AG AG LG10_PM_map_19_LEnd 1011673 T CT CT ... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. Shell Programming and Scripting

How to remove alphabets/special characters/space in the 5th field of a tab delimited file?

Thank you for 4 looking this post. We have a tab delimited file where we are facing problem in a lot of funny character. I have tried using awk but failed that is not working. In the 5th field ID which is supposed to be a integer only of that file, we are getting corrupted data as below. I... (12 Replies)
Discussion started by: Srithar
12 Replies

4. Shell Programming and Scripting

Remove Last field from a delimited file

Hi, I have a '~' delimited file and i want to remove the last field using awk. Please find the sample records below: 1428128~1~0~1100426~003~50220~005~14~0~194801~11~0~3~14~0~50419052335~0~0820652001~2~00653862 ~0~1~0~00126~1~20000110~20110423~R~ ~0~Z~1662.94~ ~002041~0045~Z~... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

5. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

6. Shell Programming and Scripting

sed command to remove the first field from a '|' delimited file

Hi I have a file with fields delimited by |. I need to remove the first field from the file. I tried cut but it just extracts that field. sample.output abc|100|name1 cde|200|name2 efg|300|name3 Output should be sample.output 100|name1 200|name2 300|name3 thanks Var (6 Replies)
Discussion started by: var285
6 Replies

7. Shell Programming and Scripting

Trying to use sed to remove the value of one field from another field

I'm trying to use sed to remove the value of one field from another field. For example: cat inputfile 123|ABC|Generic_Textjoe@yahoo.com|joe@yahoo.com|DEF 456|GHI|Other_recordjohn@msn.com|john@msn.com|JKL 789|MNO|No_Email_On_This_One|smith@gmail.com|PQR I would like to remove the email... (2 Replies)
Discussion started by: bribri87
2 Replies

8. Shell Programming and Scripting

Remove field starting with * and #

Hi i have file with data: abc,*xyz,#abc 123,#123,1234 *123,#123,abc So i want to remove only fields starting with * and # and i want output as: abc,, 123,,1234 ,,abc Please suggest something Thanks Sumit (3 Replies)
Discussion started by: sumit207
3 Replies

9. Shell Programming and Scripting

remove last field from string

Hi everybody! I want to cut the last field from string, can anybody help me?? String input:: /abc1/abc2/abc3 output:: /abc1/abc2 Regards, Kiran (2 Replies)
Discussion started by: dddkiran
2 Replies

10. Shell Programming and Scripting

Remove spaces from first field, and write entire contents into other text file

Hi all, I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need. I use the following command to remove the first line... (3 Replies)
Discussion started by: carriehoff
3 Replies
Login or Register to Ask a Question