How to delete the last word in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete the last word in a file?
# 1  
Old 09-13-2010
How to delete the last word in a file?

Hi All,
I have a file with the data as below. In this i want to delete the last word. Could you pls help me.

$INSTALL_HOME/lib/fm_voucher_pol.so
$INSTALL_HOME/source/sys/fm_apn_pol/fm_apn_pol_device_set_state.c

In the above two lines i want to delete fm_voucher_pol.so and fm_apn_pol_device_set_state.c

PS: The field postion of the last word is not known, it may differ line to line.


Thanks in advance,
Giri
# 2  
Old 09-13-2010
One in sed:
Code:
$ sed "s/[^/]*$//" file1
$INSTALL_HOME/lib/
$INSTALL_HOME/source/sys/fm_apn_pol/

# 3  
Old 09-13-2010
Try this (backup the file first):
Code:
perl -pi -e "s/\/[^\/]+$/\/\n/" file

# 4  
Old 09-13-2010
use "dirname"....

dirname $INSTALL_HOME/lib/fm_voucher_pol.so
result: $INSTALL_HOME/lib

dirname $INSTALL_HOME/source/sys/fm_apn_pol/fm_apn_pol_device_set_state.c
result: $INSTALL_HOME/source/sys/fm_apn_pol
# 5  
Old 09-13-2010
Thanks a lot Guys, it worked!!!....
# 6  
Old 09-13-2010
Code:
str=$INSTALL_HOME/lib/fm_voucher_pol.so
echo ${str%/*}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete word from the file

My file has contents like below. It is named as output_file.txt /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs/SASApp_STPServer_2015-08-27_tmptcmsaslva2_19142.log... (12 Replies)
Discussion started by: Ram Kumar_BE
12 Replies

2. Shell Programming and Scripting

Delete Word between two Char.

HI Guys, I have Input:- A nput A.txt 000100/port_xcu_dev_6/port_0_dev_7 000100/port_xcu_dev_6/port_1_dev_10 000100/port_xcu_dev_2/port_2_dev_8 000100/port_xcu_dev_3/port_3_dev_11 000100/port_xcuv_9/port_4_dev_9 ... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

4. UNIX for Dummies Questions & Answers

Script to delete a word based on position in a file

Hi, I am new to unix. I want to delete 2 words placed at position say for example at 23rd and 45th position in a line. I used sed but couldnt achieve this. Example: the file contains 2 lines 12345 98765 "12345" 876 12345 98765 "64578" 876 I want to delete " placed at position 13 and 19... (4 Replies)
Discussion started by: nbks2u
4 Replies

5. Shell Programming and Scripting

Delete everything after/before a word in a file

I'm looking for a command that will read a file listing information and delete everything after a certain word is found. I also may need to search the file and delete everything before a certain word. The file would contains fields of information like below repeating for the entire file; Name... (5 Replies)
Discussion started by: daveisme
5 Replies

6. Shell Programming and Scripting

Delete character from a word

Friends, I'm looking for a command that delete the first tho caractere in a word. Here is an exp : I want to replace "20091001" by "091001" or "replace" by "place" Thx, (13 Replies)
Discussion started by: newpromo
13 Replies

7. Shell Programming and Scripting

To find a word in a file and delete the file

Hi, I need to find a word in a file, and I need to delete the file where the word resides on the file. I did try with the grep -H "word" using the find command, but no luck. (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

Delete repeated word in text file

Hi expert, I am using C shell. And i trying to delete repeated word. Example file.txt: BLUE YELLOW RED VIOLET RED RED BLUE WHITE YELLOW BLACK and i wan store the output into a new file: BLUE (6 Replies)
Discussion started by: vincyoxy
6 Replies

9. UNIX for Dummies Questions & Answers

Delete a word

i have created the following input script CREATE OR REPLACE VIEW MMSSENDAVGUPLOADTIME_GN AS sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_val sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_counter select... (1 Reply)
Discussion started by: gseptember
1 Replies

10. Shell Programming and Scripting

How to delete a word from a file?

Hi All, I want to delete a word from file. How to do that. I have file that contains the following Information. EntityName:alba00r1.mis.amat.com OverallStatus:Minor IfName:Gi1/0 EntityName:alba00r1.mis.amat.com ] OverallStatus:Normal IfName:Se0/0/0... (4 Replies)
Discussion started by: ntgobinath
4 Replies
Login or Register to Ask a Question