- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using sed to substitute along a line between c...
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
Forums
Discussions
Discussions
Discussions
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
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
12-21-2005 02:59 AM
12-21-2005 02:59 AM
0215-04@10648218001105122@RRRR0000@002000@
123456789012345678901234567890123456789012
1 2 3 4
In this example I would like to substitute the @ in positions 26 & 35 with a 9 from positions 20 40 only. Leaving the @ at posiiton 8 unchanged. Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 03:51 AM
12-21-2005 03:51 AM
Re: Using sed to substitute along a line between column positions
If I understand correctly, you want to substitute the "@" character in positions 26 & 35 with a "9". This would work:
# perl -pe 's/(.{25,25})@(.+)/\1_\2/;s/(.{34,34})@(.+)/\1_\2/;s/_/9/g'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 04:00 AM
12-21-2005 04:00 AM
Re: Using sed to substitute along a line between column positions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 04:37 AM
12-21-2005 04:37 AM
Re: Using sed to substitute along a line between column positions
Here is the sed approach...
# sed -e 's/@/9/2' -e 's/@/9/2'
cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 05:52 AM
12-21-2005 05:52 AM
Solutioncreate a file called useme.awk
call it as follows:
awk -f useme.awk < data > fixed_data
useme.awk should look like:
{l1=$0;
for (idx1=20;idx1<=40;idx1++)
{if (substr(l1,idx1,1)=="@") {l1a=substr(l1,1,idx1-1);l3a=substr(l1,idx1+1);
l1=l1a"9"l3a;}
}
print l1;
}
best regards,
Oz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 06:16 AM
12-21-2005 06:16 AM
Re: Using sed to substitute along a line between column positions
It occurs to me from your description that you might not always want to substitute a "9" character, but rather whatever is in position 20 and 40 into 26 and 35, respectively. This would accomplish that:
# cat my.pl
#!/usr/bin/perl
while (<>) {
$c20=substr($_,20,1);
$c40=substr($_,40,1);
s/(.{25})@(.+)/\1$c20\2/;
s/(.{34})@(.+)/\1$c40\2/;
print;
}
run as:
# my.pl file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2005 06:31 AM
12-21-2005 06:31 AM
Re: Using sed to substitute along a line between column positions
# perl -pe'substr($_,25,10)=~s/\@/9/g' your_file
will replace all '@' characters in every line from position 26 (perl's offsets start at 0) over a length of 10 into '9's. You can also use tr// and save one character
Demo:
lt09:/home/merijn 112 > cat xx.txt
0215-04@10648218001105122@RRRR0000@002000@
lt09:/home/merijn 113 > perl -pe'substr($_,25,10)=~s/\@/9/g' xx.txt
0215-04@106482180011051229RRRR00009002000@
lt09:/home/merijn 114 > perl -pe'substr($_,25,10)=~tr/@/9/' xx.txt
0215-04@106482180011051229RRRR00009002000@
lt09:/home/merijn 115 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2005 09:35 PM
12-22-2005 09:35 PM