-   Programming (
-   -   Shell Script to Delete part text of a line if pattern matches (

harsha1980 04-12-2010 10:52 AM

Shell Script to Delete part text of a line if pattern matches

I am trying to create a shell script, on taking a input file as parameter, which need to do 3 things

1) create a copy of existing file.
2) add a new line to the copied file.
3) strip off all the absolute paths inside the copied file

The first 2 points are straight forward. but i am finding it difficult to acheive the 3rd point. myself not very good with awk and sed. but gave it a shot in vain.

For example, the input script consists of below,

PROGRAM=`/usr/bin/basename $0`
HOST=`/usr/bin/uname -n`
echo Start $PROGRAM `/usr/bin/date` | \
/usr/bin/tee -a $LOG

The output of the script should look like,

PROGRAM=`basename $0`
HOST=`uname -n`
echo Start $PROGRAM `date` | \
tee -a $LOG

Your help is highly appreciated.


crts 04-12-2010 10:59 AM


sed -i 's|/.*/||g' file

harsha1980 04-12-2010 11:25 AM

Hi crts

THat worked like a charm. Thanks a million. :D


grail 04-12-2010 09:11 PM

Don't forget to mark as SOLVED.

harsha1980 04-13-2010 06:39 AM

Hello crts and others

The solution crts gave me works fine. But i have another problem. my script contains other custom paths which are also getting stripped off which i dont want.

From the following,
PROGRAM=`/usr/bin/basename $0`

i want the absolute path stripped off from first line(PROGRAM=`/usr/bin/basename $0`) only and not from the second line(CFG_DIR=/u08/xxx/conf)

In a nutshell, i want the system paths to be stripped off and not the custom paths. As i will be inserting PATH variable into the file, i want to strip off the absolute paths of the system commands.

Hope i am clear. THnx

PMP 04-13-2010 06:48 AM


sed -i 's|/usr.*/||g' file
Its better to keep the backup of the file before using sed inplace replacement.

harsha1980 04-13-2010 06:54 AM


Many THnx for immediate reply.

And THnx for the solution. Just one more query. If i have more than one path to be included in the search pattern, how do i do it?

FOr example,

to strip of path like '/usr/bin' your answer above works perfect.
what if i want to strip off more than one path type like 'usr/bin', '/sbin/', '/bin/'

Your help is highly appreciated.

Thanks & Regards

crts 04-13-2010 06:55 AM


sed -i '/^CFG_DIR/! s|/.*/||g' test
This will ommit every line that starts with 'CFG_DIR'

PMP 04-13-2010 07:02 AM


sed -i 's#/usr.*/\|/sbin.*/\|/bin.*/##g' file

harsha1980 04-13-2010 10:07 AM

Hello crts and PMP

THanks for your reply and solutions. THey were really helpful.

The code works fine except with this particular line. After running the code the only unexpected change was as follows...

< /usr/bin/cp $MAIL_FILE /tmp/$ALERT

the expected difference was supposed as,

< /usr/bin/cp $MAIL_FILE /tmp/$ALERT
> cp $MAIL_FILE /tmp/$ALERT

Kindly let me know where is it going wrong?


crts 04-13-2010 10:19 AM


sed -i '/^CFG_DIR/! s|/[^ ]*/||' test

harsha1980 04-13-2010 10:44 AM

Hello crts

i understand what you are saying.

From what i see, the code is reading the whole line and stripping off the data till the last '/' in the line.

I can put an exception using your solution, but then i need this code to be executed on approx 100 scripts which will have similar exceptions. It'll be very time consuming and cumbersome to find and accommodate all of them.

On the other side, I see this problem ONLY occurs when there is more than 1 command with absolute path in the SAME line.(in the above example '/usr/bin/cp' and '/tmp/$ALERT')

So how can we tune the code to consider the stripping to end when it finds a word between of last '/' of the string and 'space', and continue reading the remaining line for further seperate stripping.

Hope i made the scenario clear.

Your help is highly appreciated.


crts 04-13-2010 11:15 AM


I am not sure what exactly the problem is. If you cannot say what these exceptions look like and if there is no general pattern that all this exceptions have in common then I am afraid that your task cannot be achieved with a few lines of code. From your description so far I take it that in one script it is the line which contains 'CFG_DIR' that has to remain unchanged and in the next script this variable is named differently. You might have to create a more advanced script that can deal with the complexity of the task.

Here is a good starting point:

grail 04-13-2010 10:08 PM

OP - Just to confirm, you have noticed the difference between the first and second example provided by crts?

#old one
sed -i '/^CFG_DIR/! s|/.*/||g' test
#new one
sed -i '/^CFG_DIR/! s|/[^ ]*/||' test

1. New is no longer global, represented by "g" in old
2. Old searches greedily between // where as second uses [^ ] to say there should be no spaces between //

I have copied all of your entries from different posts above and placed in a file and the new code returns what you have

PMP 04-14-2010 12:06 AM

It is better if you explain what mentioned by crts. With all the possible scenarios you mentioned here the following solution should work.


sed 's#\(/usr\|/sbin\|/bin\)[^ ]*/##g' myfile
And is there any specific reason for removing the absolute path ?

All times are GMT -5. The time now is 10:22 AM.