LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-27-2008, 10:32 AM   #1
maker10
LQ Newbie
 
Registered: Aug 2003
Posts: 15

Rep: Reputation: 0
Problems with sed


Hi All,

I am trying to create a sed script that will locate text at the beginning of a line (MOZ_ARCH=$(uname -m)); insert a comment at the beginning of the line (#MOZ_ARCH=$(uname -m); and then append the line with (MOZ_ARCH=$(i686)

The OS is x86_64 RHEL5 Workstation using the input file /usr/bin/firefox.

Here is my script and the output:

[root@cels159075 bin]# sed 's/MOZ_ARCH=/#MOZ_ARCH/' /usr/bin/firefox | grep \#MOZ_ARCH

#MOZ_ARCH$(uname -m)

As you can see this is part of what I"m trying to accomplish, but I don't know how to append the line with MOZ_ARCH=$(i686)

Any help would be very appreciated.

Thanks
 
Old 06-27-2008, 10:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try to substitute the entire line with two lines, including a newline character in the replacement string:
Code:
 sed 's/MOZ_ARCH=$(uname -m)/#MOZ_ARCH=$(uname -m)\nMOZ_ARCH=$(i686)/' file
or append a line with the 'a' command after the matching pattern:
Code:
sed 's/MOZ_ARCH=$(uname -m)/#MOZ_ARCH=$(uname -m)/; /MOZ_ARCH=$(uname -m)/ a MOZ_ARCH=$(i686)' file
 
Old 06-27-2008, 10:50 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
sed 's/^MOZ_ARCH=.*/#& MOZ_ARCH=$(i686)/' /usr/bin/firefox

But I can't tell what you really want. Do you want to change the value after MOZ_ARCH= or add another MOZ_ARCH entry?
Please post the actual line and how you want it to change.

Or do you want to add MOZ_ARCH=$(i686) on the next line?
Code:
 cat tempfile
MOZ_ARCH=$(x86_64)
MOZ_LIB=/usr/lib
jschiwal@hpamd64:~> sed '/^MOZ_ARCH/{s//#&/
                                   a\
MOZ_ARCH=$(x86)
}' tempfile
#MOZ_ARCH=$(x86_64)
MOZ_ARCH=$(x86)
MOZ_LIB=/usr/lib
Without the -i option the file isn't actually changed but just displayed. Either redirect the file to a temp file and then replace the current file or use the -i option. I'd recommend not using -i until after you are sure the sed command does what you want. Also backup the original just in case.
 
Old 06-27-2008, 01:39 PM   #4
maker10
LQ Newbie
 
Registered: Aug 2003
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks All

Hi All,

Thanks for the quick response and all of your suggestions. Colucix's suggestion was exactly what I was looking for.

sed 's/MOZ_ARCH=$(uname -m)/#MOZ_ARCH=$(uname -m)\nMOZ_ARCH=$(i686)/' file

Thanks
 
  


Reply



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
Problems with shell scripting and sed Uzelth Programming 5 04-13-2008 06:21 PM
sed RegEx problems InJesus Programming 6 01-12-2007 11:48 AM
SED has problems with Ram rabby Linux - Software 5 02-28-2005 06:46 PM
sed problems icisyd Linux - Software 2 11-19-2004 06:17 PM
weird sed problems C++freak Slackware 5 08-04-2003 04:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration