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
# 8  
Old 03-05-2010
Another one:
Code:
sed 's/\(.*\) .*/\1/' file

# 9  
Old 03-05-2010
rdcwayx' awk solution is best suited here, as the correct sed alternative is more complicated:
Code:
sed 's/[[:space:]]*[^[:space:]]*$//' infile

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

# 10  
Old 03-05-2010
Quote:
Originally Posted by pludi
Here's a Perl solution:
Code:
$ 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?
Sorry, a bit quick with my answer. Here is another awk solution:

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

# 11  
Old 03-06-2010
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.
# 12  
Old 03-06-2010
Quote:
Originally Posted by vivekraj
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.
# 13  
Old 03-06-2010
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.
# 14  
Old 03-06-2010
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.
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