- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk with sub
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
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
тАО08-12-2002 04:49 AM
тАО08-12-2002 04:49 AM
I have a file thats looks like this
111 222 0000 9999 RABC:AB fat 1 3 5
111 223 0000 9999 RABC:AC cat 1 34 5
What I would like to do is sub(0000,XXXX && sub(9999,XXXX) when awk {if($5 == "???:A[A-Z]")
How can I put this in the awk statement.
Thanks in advanced
Chris
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 04:53 AM
тАО08-12-2002 04:53 AM
Re: Awk with sub
Please just give us some input and output, then we will write you the blackbox.
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 05:12 AM
тАО08-12-2002 05:12 AM
Re: Awk with sub
Search in the file, and when a pattern RABC:AC is found for eg RABC:AB or RABC:AC or ????:A[A-Z] is found, then sub 0000 99999 to XXXX for 0000 and XXXX for 9999. The result should look like this
111 222 XXXX XXXX RABC:AB fat 1 3 5
111 223 XXXX XXXX RABC:AC cat 1 34 5
Thanks Once again
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 05:55 AM
тАО08-12-2002 05:55 AM
Re: Awk with sub
Try this awk command-
awk '$5~/....:A[A-Z]/{$3="XXXX"; $4="XXXX" ; print $0}' yourinputfile.txt
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 05:58 AM
тАО08-12-2002 05:58 AM
Re: Awk with sub
awk '{
sub(/0000/,"XXXX");
sub(/9999/,"XXXX");
}' input
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 06:09 AM
тАО08-12-2002 06:09 AM
SolutionHere is another version, that prints all lines, but only changes those lines that match the pattern.
awk '$5~/....:A[A-Z]/{$3="XXXX"; $4="XXXX"};{print $0}' yourinputfile.txt
Hope this helps...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 11:08 AM
тАО08-12-2002 11:08 AM
Re: Awk with sub
Great stuff. I tried this one with results which is incorrect. Could you PLEASE explain why.
cat FILE | awk -v var="[0-9][0-9][0-9][0-9]" -v var2="XXXX" '{if ($5 == "....:A[A-Z]");{sub(var1,var2,$3)};{print $0}}' | more
Many Thanks to All
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2002 01:18 PM
тАО08-12-2002 01:18 PM
Re: Awk with sub
In your sample you set "var=". Did you mean var1= ?
You are also doing a comparsion using "$5 ==", this should be "$5 ~" so that it does a regular expression compare.
The regular expression I believe needs to be enclosed with "/".
Remove the ";" after the if ($5 ... ).
Try this-
cat FILE | awk -v var1="[0-9][0-9][0-9][0-9]" -v var2="XXXX" '{if ($5 ~ /....:A[A-Z]/){sub(var1,var2,$3)};{print $0}}' | more
Hope this Helps.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2002 12:58 PM
тАО08-13-2002 12:58 PM
Re: Awk with sub
Thanks for all you help, it was greatly appreciated.
Chris