LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-12-2010, 10:52 AM   #1
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Rep: Reputation: 0
Shell Script to Delete part text of a line if pattern matches


Hi

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.

Regards
 
Old 04-12-2010, 10:59 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Code:
sed -i 's|/.*/||g' file
 
1 members found this post helpful.
Old 04-12-2010, 11:25 AM   #3
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi crts

THat worked like a charm. Thanks a million.

Regards
 
Old 04-12-2010, 09:11 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Don't forget to mark as SOLVED.
 
Old 04-13-2010, 06:39 AM   #5
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Original Poster
Rep: Reputation: 0
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`
CFG_DIR=/u08/xxx/conf

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

Last edited by harsha1980; 04-13-2010 at 06:41 AM. Reason: clarification
 
Old 04-13-2010, 06:48 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
sed -i 's|/usr.*/||g' file
Its better to keep the backup of the file before using sed inplace replacement.
 
Old 04-13-2010, 06:54 AM   #7
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Original Poster
Rep: Reputation: 0
HI PMP

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
 
Old 04-13-2010, 06:55 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Code:
sed -i '/^CFG_DIR/! s|/.*/||g' test
This will ommit every line that starts with 'CFG_DIR'
 
Old 04-13-2010, 07:02 AM   #9
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
sed -i 's#/usr.*/\|/sbin.*/\|/bin.*/##g' file
 
Old 04-13-2010, 10:07 AM   #10
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Original Poster
Rep: Reputation: 0
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
---
> $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?

Regards
 
Old 04-13-2010, 10:19 AM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Code:
sed -i '/^CFG_DIR/! s|/[^ ]*/||' test
 
Old 04-13-2010, 10:44 AM   #12
harsha1980
LQ Newbie
 
Registered: Apr 2010
Posts: 17

Original Poster
Rep: Reputation: 0
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.

Regards
 
Old 04-13-2010, 11:15 AM   #13
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

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:
http://tldp.org/
 
Old 04-13-2010, 10:08 PM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
OP - Just to confirm, you have noticed the difference between the first and second example provided by crts?
Code:
#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
requested.
 
Old 04-14-2010, 12:06 AM   #15
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

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

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

Last edited by PMP; 04-14-2010 at 12:08 AM. Reason: addtion of specific path clause
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 12:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration