- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: awk formatting output
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
03-11-2005 12:26 AM
03-11-2005 12:26 AM
can anyone help me format the following file:
time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field3: a b field2: bb
..into:
time: 20050310 field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field2: bb field3: a b
.. because the order and number of fields is not fixed I am struggling to get awk to pull the right data out - any ideas?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 12:51 AM
03-11-2005 12:51 AM
Re: awk formatting output
It's not clear what the 'rules' are for re-structuring your output. Is each line structured/cut differently?
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:00 AM
03-11-2005 01:00 AM
Re: awk formatting output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:01 AM
03-11-2005 01:01 AM
Re: awk formatting output
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:08 AM
03-11-2005 01:08 AM
Re: awk formatting output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:28 AM
03-11-2005 01:28 AM
Re: awk formatting output
I think the sed will only remove the field name (and not its data) and the sort will then not work because the number of fields will be different. Also, I cannot guarantee the position of field2 and field3 (see the final line of the example), so this means a sort won't work.
Alec
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:40 AM
03-11-2005 01:40 AM
Re: awk formatting output
Note - field1: . in sed command.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 01:46 AM
03-11-2005 01:46 AM
Re: awk formatting output
lt09:/tmp 107 > cat xx
time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field3: a b field2: bb
lt09:/tmp 108 > perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}sort keys%f' xx
time: 20050310 field1: a field2: b b field3: c
time: 20050311 field2: a b
time: 20050311 field2: bb field3: a b
lt09:/tmp 109 >
Enjoy, Have FUN! H.Merijn
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 03:47 AM
03-11-2005 03:47 AM
Re: awk formatting output
He doesn't want "field1" included with the output.
So how about modifying your routine as follows-
perl -lpe'@f=split/\s+(field\d+:)/;$i=shift@f;%f=@f;delete $f{"field1:"};$_=join" ",$i,map{"$_$f{$_}"}sort keys%f' xx
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2005 05:08 AM
03-11-2005 05:08 AM
SolutionYour modification also changes the output, since I included the whitespace leading towards the fields in the separator, and you took it out. That indeed makes it easier to delete the key field1:, but it also glues the fields together in a different way. You solved that by adding the space in the join, but it still is different from my original plan.
So /my/ modification to filer field1:'s would be
# perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}grep!/\bfield1:/,sort keys%f' xx
There is still a small weakness in there if there are more than 9 keys (1,10,11,12,2,3,4,5,6,7,8,9), which could be schwarzian'd
# perl -lpe'@f=split/(\s+field\d+:)/;$i=shift@f;%f=@f;$_=join"",$i,map{"$_$f{$_}"}grep!/\bfield1:/,map{$_->[0]}sort{$a->[1]<=>$v->[1]}map{/(\d+)/;[$_;$1]}keys%f' xx
But now we're getting towards the point where you'd normaly turn from a one-liner to a script, and document what you're doing
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2005 08:44 PM
03-13-2005 08:44 PM