sed command to remove a word from string | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sed command to remove a word from string

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 24 December 2013, 7:27 AM EST
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
sed command to remove a word from string

Hello All,
I am running a command
Code:
find . -name amp.cfg | cut -c 3-

which gives me output something like below

Code:
rel/prod/amp.cfg
rel/fld/amp.cfg
deb/detail/amp.cfg
deb/err/amp.cfg

I want to remove trailing "/amp.cfg" so that i should get output something like below.

Code:
rel/prod
rel/fld
deb/detail
deb/err

.
I tried tr command
Code:
tr -d 'amp.cfg'

but had no luck.


Regards,
Anand Shah
Sponsored Links
    #2  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts

Code:
sed 's/\/amp.cfg$//' file

Sponsored Links
    #3  
Old 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 24 December 2013, 7:27 AM EST
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
Dear Pamu,
As I told in my post, i am using piped output from previous commands as input to sed so when i run this command it gives me No such file or directory error.

I also tried
Code:
sed `s/\/\amp.cfg$//`

    #4  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
Quote:
Originally Posted by anand.shah View Post


Code:
sed `s/\/\amp.cfg$//`

"`" won't work here....

try


Code:
find . -name amp.cfg | cut -c 3- | sed 's/\/amp.cfg$//'

The Following User Says Thank You to pamu For This Useful Post:
anand.shah (12-11-2012)
Sponsored Links
    #5  
Old 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 24 December 2013, 7:27 AM EST
Posts: 37
Thanks: 9
Thanked 0 Times in 0 Posts
Dear Pamu,
Thank you.Its working fine.
I was using "`" instead of "'".My mistake.
Can u plz tell me the use of "$" as it is working fine without that ?

Last edited by anand.shah; 12-11-2012 at 01:09 AM..
Sponsored Links
    #6  
Old 12-11-2012
kalpeer kalpeer is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 1 April 2014, 6:00 AM EDT
Location: Pune ,India
Posts: 148
Thanks: 12
Thanked 25 Times in 25 Posts
Quote:
find . -name amp.cfg | cut -c 3- | sed 's/\/amp.cfg$//'
"$" denote for the end of the line

Below command is also another way to match your pattern

Quote:
find . -name amp.cfg | sed 's:^./\(.*\)/amp.cfg$:\1:'

Last edited by kalpeer; 12-11-2012 at 06:48 AM..
Sponsored Links
    #7  
Old 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
Quote:
Originally Posted by anand.shah View Post
Can u plz tell me the use of "$" as it is working fine without that ?
$ is used for a line which ends with amp.cfg.
Without using $ it will replace first entry of the line

Please check..


Code:
$ cat file
rel/prod/amp.cfg
rel/fld/amp.cfg
rel/fld/amp.cfg/sdd
rel/fld/amp.cfg/doc/cam/amp.cfg


Code:
$ sed 's/\/amp.cfg$//' file
rel/prod
rel/fld
rel/fld/amp.cfg/sdd
rel/fld/amp.cfg/doc/cam


Code:
$ sed 's/\/amp.cfg//' file
rel/prod
rel/fld
rel/fld/sdd
rel/fld/doc/cam/amp.cfg

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed or awk command to replace a string pattern with another string based on position of this string vivek d r Shell Programming and Scripting 10 06-19-2012 09:35 AM
Using SED to extract a word or string. simpletech369 Shell Programming and Scripting 2 01-25-2012 11:42 AM
remove newline between two string with sed command in unix shellscript jcrshankar Shell Programming and Scripting 4 02-21-2011 05:46 PM
awk or sed command to print specific string between word and blank space elamurugu Shell Programming and Scripting 5 12-06-2010 09:49 AM
how to remove spaces in a string using sed. radhika Shell Programming and Scripting 4 06-02-2005 03:00 PM



All times are GMT -4. The time now is 10:03 PM.