Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

sed command to remove a word from string

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 20 September 2014, 1:16 AM EDT
Posts: 45
Thanks: 11
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 Unix and Linux 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 27 May 2015, 11:47 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 20 September 2014, 1:16 AM EDT
Posts: 45
Thanks: 11
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 Unix and Linux 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 27 May 2015, 11:47 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 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 Unix and Linux 12-11-2012
anand.shah anand.shah is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 20 September 2014, 1:16 AM EDT
Posts: 45
Thanks: 11
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 Unix and Linux 12-11-2012
kalpeer kalpeer is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 24 June 2015, 3:50 PM EDT
Location: Pune ,India
Posts: 152
Thanks: 14
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 Unix and Linux 12-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 27 May 2015, 11:47 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:46 PM.