- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sed - print old line and new changed line
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
тАО01-27-2009 02:40 AM
тАО01-27-2009 02:40 AM
i have a file "TEST" and want to change the digit(s) after "=" . but i also want to print the old entry with a comment (for information).
i want to use "sed", is it possible ?
file:
TEST = 10 # comment default value: 0
sed , with "p" , i can print the old entry, but i want to change it with "#"
sed -e "/^\([ \t]*TEST[ \t]*=[ \t]*\)/ {
p
s/^\([ \t]*TEST[ \t]*=[ \t]*\)\([0-9]*\)\([ \t]*\)/\199\3/p
}" TEST
the new output should be:
#TEST = 10 # comment default value: 0
TEST = 99 # comment default value: 0
regards,tom
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 05:03 AM
тАО01-27-2009 05:03 AM
Re: sed - print old line and new changed line
I'd use Perl:
# perl -pe '$match=qr{(.*TEST\s*=\s*)(10\b)};m{$match} and print and s{10}{99}' file
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 05:38 PM
тАО01-27-2009 05:38 PM
SolutionYes, but it requires work, where perl and awk it would be more obvious.
================================================
sed '
/^\([ \t]*TEST[ \t]*=[ \t]*\)/ {
h; # save
s/^/#/
p
g; # restore
s/^\([\t]*TEST[\t]*=[\t]*\)\([0-9]*\)\([\t]*\)/\199\3/
} ' TEST > TEST.new
Note you can't really use \t for a tab.
I had to remove the space on the last "s" line so it wouldn't break.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-27-2009 09:27 PM
тАО01-27-2009 09:27 PM
Re: sed - print old line and new changed line
Actually, this variation is a bit "safer" insofar as it avoids potentially superious substitutions:
# perl -pe '$match=qr[(.*TEST\s*=\s*)(10)(\b)];m[$match] and print and s[$qr][${1}99]' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2009 02:25 AM
тАО01-28-2009 02:25 AM
Re: sed - print old line and new changed line
thanks a lot for your "sed" solution and i use following character [:space:] for whitespace/blank. I hope that was also in your interest. now "sed" works well.
sed '
/^\([[:space:]]*TEST[[:space:]]*=[[:space:]]*\)/ {
h; # save
s/^/#/
p
g; # restore
s/^\([[:space:]]*TEST[[:space:]]*=[[:space:]]*\)\([0-9]*\)\([[:space:]]*\)/\199\3/
} ' TEST > TEST.new
regards,tom