- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to find and replace multiple lines.
Operating System - HP-UX
1752815
Members
6447
Online
108789
Solutions
Forums
Categories
Company
Local Language
юдл
back
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
юдл
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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
тАО09-07-2010 11:05 PM
тАО09-07-2010 11:05 PM
How to find and replace multiple lines.
Hello.
I have a input file containing multiple sets of following text:
----------------
:59:/1234567890
LINE1
LINE2
LINE3
:50:/9856958654
LINE1
LINE2
----------------
Basically, Tag 59 and Tag50 can have any number of lines in the input and any number against it. I want to write a script so that tag50 and tag59 could be found and the strings could be replaced as below:
----------------
:59:/XXXXXXXXXX
ZZZZZZZZ
ZZZZZZZZ
ZZZZZZZZ
:50:/XXXXXXXXXX
ZZZZZZZZ
ZZZZZZZZ
----------------
Your help will be highly appreciated. Thank you.
I have a input file containing multiple sets of following text:
----------------
:59:/1234567890
LINE1
LINE2
LINE3
:50:/9856958654
LINE1
LINE2
----------------
Basically, Tag 59 and Tag50 can have any number of lines in the input and any number against it. I want to write a script so that tag50 and tag59 could be found and the strings could be replaced as below:
----------------
:59:/XXXXXXXXXX
ZZZZZZZZ
ZZZZZZZZ
ZZZZZZZZ
:50:/XXXXXXXXXX
ZZZZZZZZ
ZZZZZZZZ
----------------
Your help will be highly appreciated. Thank you.
- Tags:
- awk
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-08-2010 12:37 AM
тАО09-08-2010 12:37 AM
Re: How to find and replace multiple lines.
>Tag 59 and Tag50 can have any number of lines in the input and any number against it.
So if you see :50: or :59: you want to replace the string on that line and up to a line with :XX: with "X" then "Z"?
Perhaps something like this:
awk '
BEGIN {
str1 = "/XXXXXXXXXX"
str2 = "ZZZZZZZZ"
replace = 0
}
substr($0, 1, 4) == ":50:" ||
substr($0, 1, 4) == ":59:" {
replace = 1
print substr($0, 1, 4) str1
next
}
substr($0, 1, 1) == ":" { # new tag?
replace = 0
}
replace {
print str2
next
}
{
print $0 # copy line
}' file
So if you see :50: or :59: you want to replace the string on that line and up to a line with :XX: with "X" then "Z"?
Perhaps something like this:
awk '
BEGIN {
str1 = "/XXXXXXXXXX"
str2 = "ZZZZZZZZ"
replace = 0
}
substr($0, 1, 4) == ":50:" ||
substr($0, 1, 4) == ":59:" {
replace = 1
print substr($0, 1, 4) str1
next
}
substr($0, 1, 1) == ":" { # new tag?
replace = 0
}
replace {
print str2
next
}
{
print $0 # copy line
}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-08-2010 05:59 AM
тАО09-08-2010 05:59 AM
Re: How to find and replace multiple lines.
Hi:
Here's a solution based on your requirements' definition:
# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
local $/ = undef;
my @pieces = split( /(?=:\d+:\/.+)/, <> );
for (@pieces) {
my @subpieces = split( /$/m, $_ );
if (/^(:(50|59):\/)/) {
print $1, "XXXXXXXXXX\n";
shift @subpieces;
print "ZZZZZZZZZ\n" for ( 1 .. @subpieces - 1 );
}
else {
print $_;
}
}
1;
...run as:
# ./filter file
Regards!
...JRF...
Here's a solution based on your requirements' definition:
# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
local $/ = undef;
my @pieces = split( /(?=:\d+:\/.+)/, <> );
for (@pieces) {
my @subpieces = split( /$/m, $_ );
if (/^(:(50|59):\/)/) {
print $1, "XXXXXXXXXX\n";
shift @subpieces;
print "ZZZZZZZZZ\n" for ( 1 .. @subpieces - 1 );
}
else {
print $_;
}
}
1;
...run as:
# ./filter file
Regards!
...JRF...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP