- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Best sed usage
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 06:43 AM
тАО05-12-2008 06:43 AM
I have 3 different parameters to change in a file and want to use sed, what is the best way to change the value of 3 different strings from the same file - then replace the file ie:
FILE=FILE.cfg
userbase=xyz
timeout=5
defaultentry=local
# cat $FILE | sed -e 's/userbase=xyz/userbase=abc/;
-e s/timeout=5/timeout=60/ -e s/defaultentry=local/defaultentry/' > ${FILE}.tmp
cp ${FILE}.tmp ${FILE}
is this the best way or is there a more logical method?
thanks
Chris
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 06:52 AM
тАО05-12-2008 06:52 AM
SolutionIf you want to do this with 'sed', do:
# sed -e 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' ${FILE} > ${FILE}.tmp
# mv ${FILE}.tmp ${FILE}
Notice that you do NOT need to run a 'cat' process which 'sed' then reads from a pipe. This is a waste of a process.
Notice too the change I made in the 'sed' syntax. A semicolon between the substitution commands with one '-e' switch ahead of the arguments.
Of course, Perl can do an inplace update eliminating the 'mv' step :-)
Regards!
...JRF...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 07:22 AM
тАО05-12-2008 07:22 AM
Re: Best sed usage
> syntax.
Which makes it harder to read, but does it
have any other (real) benefits?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 07:32 AM
тАО05-12-2008 07:32 AM
Re: Best sed usage
> Steven: > Notice too the change I made in the 'sed' syntax. Which makes it harder to read, but does it have any other (real) benefits?
In my opinion, this isn't harder to read. To me, is is more consistent with a script and a script's syntax (which the '-e' signifies in the first place). Moreover, the syntax becomes exactly compatabile to a Perl script:
# perl -pe 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' file
...which then leads to doing Perl in-place updates, thereby saving the 'mv step:
# perl -pi.old -e 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' file
...which preserves the original file as "file.old" while leaving the updated file as "file".
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 07:41 AM
тАО05-12-2008 07:41 AM
Re: Best sed usage
userbasedn="userbasedn:ou=people,ou=nis,ou=eu,o=unilever,c=gb"
ldaptimeout="ldaptimeout:5"
defaultentrylocation="defaultentrylocation:local"
sed -e 's/userbasedn.*$/'${userbasedn}'/' -e 's/.ldaptimeout.*$/'${ldaptimeout}'/' -e 's/^defaultentrylocation.*$/'${defaulte
ntrylocation}'/' ${LDAPCFG} > ${LDAPCFGBCK}
and this works however your syntax the command waits ie:
sed -e 's/userbasedn.*$/'${userbasedn}'/';'s/.ldaptimeout.*$/'${ldaptimeout}'/';'s/^defaultentrylocation.*$/'${defaultentrylocation}'/' ${LDAPCFG} > ${LDAPCFGBCK}
what have i missed?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 08:24 AM
тАО05-12-2008 08:24 AM
Re: Best sed usage
Ah, so you changed the example :-)
OK, if you are going to use interpolated variables, put them in double-quotes so that the shell expands them:
# sed -e "s/userbasedn.*$/"${userbasedn}"/;s/.ldaptimeout.*$/"${ldaptimeout}"/;s/^defaultentrylocation.*$/"${defaultentrylocation}"/" file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 08:41 AM
тАО05-12-2008 08:41 AM
Re: Best sed usage
Whateveryousay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 11:59 AM
тАО05-12-2008 11:59 AM
Re: Best sed usage
sed -e 's/userbase=xyz/userbase=abc/;
-e s/timeout=5/timeout=60/ -e s/defaultentry=local/defaultentry/' > ${FILE}.tmp
doesn't / wont run anyway...it doesn't understand the "-e" inside the quotes...believe the o.p. is confusing this with the "multiple pattern" in grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 04:05 PM
тАО05-12-2008 04:05 PM
Re: Best sed usage
> OldSchool: of course, the original posting...doesn't / wont run anyway...it doesn't understand the "-e" inside the quotes...believe the o.p. is confusing this with the "multiple pattern" in grep
True, but this is legal (and looks like a multiple option/multiple pattern 'grep'):
# sed -e 's/userbase=xyz/userbase=abc/' -e 's/timeout=5/timeout=60/' -e 's/defaultentry=local/defaultentry/' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-12-2008 05:54 PM
тАО05-12-2008 05:54 PM
Re: Best sed usage
Other than JRF's mv and removal of cat, I would go with multiple -e scripts that he had at the end.
I typically wouldn't use the jammed up embedded commands but having a blank after each ";" may make Steven happy. :-)
>Steven: Which makes it harder to read, but does it have any other (real) benefits?
Well, as OldSchool points out, JRF's works, and the original didn't. I would typically use multiple -e. You could put the script in a file (-f script-file) or use:
sed -e 's/userbase=xyz/userbase=abc/
s/timeout=5/timeout=60/
s/defaultentry=local/defaultentry/' ${FILE} > ${FILE}.tmp
A file wouldn't allow Lawrenzo's ${userbasedn} substitution, unless it was a here document.
- Tags:
- here doc