- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- awk Separating Records
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
тАО08-30-2007 08:55 PM
тАО08-30-2007 08:55 PM
I am trying to separate records using awk.
Below is a sample of the records in a file.
------------
1|D|hfoo01||39322|74883|service-call-master
2|call-no|143814
3|call-status|D|F
3|call-date-last-change|20-AUG-2007|30-AUG-2007
1|U|hfoo01||39322|74893|service-call-master
2|call-no|143814
3|call-user-only-num1|0.0000|23158.0000
1|I|hfoo01||39322|74893|job-cost-master
2|job-code|143814
3|jcm-project-manager|GNOHPH01|HFOO01
1|U|hfoo01||39322|74903|service-call-master
2|call-no|143814
3|call-invoice-no||S3106895
---------------------------
The record separators are,
1|I| or 1|D| or 1|U|
Here is my feeble attempt to do it.
-------------
% more 1.awk
BEGIN { FS = "\n"
RS = "\/<1\|[IDU]" }
{
print "New Record "$1", "$2", "$3", "$4",,"RT
}
# awk -f 1.awk audit.log
New Record 1|D|hfoo01||39322|74883|service-call-master, 2|call-no|143814, 3|call
-status|D|F, 3|call-date-last-change|20-AUG-2007|30-AUG-2007,,
----------------------
Appreciate help from awk gurus on the RS variable.
Thank you.
nash
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-30-2007 09:35 PM
тАО08-30-2007 09:35 PM
Solution>I am trying to separate records using awk.
Looks like you are joining them.
(You haven't defined RT?)
Here is what I have to concatenate your lines:
awk '
BEGIN { getline; save=$0 }
/^1\|[IDU]\|/ {
print save ",,"
save=$0
next
}
{
save=save $0 # concatenate lines
next
}
END { print save ",," } ' audit.log
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 12:19 AM
тАО08-31-2007 12:19 AM
Re: awk Separating Records
Try this:
# perl -0377 -ne '@a=split(/(1\|I\||1\|D\||1\|U\|)/,$_);@a;for $b (@a) {print "$b\n"}' file
...using your data, this would ouput:
1|D|
hfoo01||39322|74883|service-call-master
2|call-no|143814
3|call-status|D|F
3|call-date-last-change|20-AUG-2007|30-AUG-2007
1|U|
hfoo01||39322|74893|service-call-master
2|call-no|143814
3|call-user-only-num1|0.0000|23158.0000
1|I|
hfoo01||39322|74893|job-cost-master
2|job-code|143814
3|jcm-project-manager|GNOHPH01|HFOO01
...
This reads your whole file into memory (slurps it) without regard to any record seperator. It then leverages 'split' where regular expresssion patterns can be used.
The fact that your pattern to match contains the pipe (vertical bar) and alternation uses that symbol too, means that we have to escape with backslashes making a rather ugly expression.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 12:54 AM - edited тАО09-24-2011 05:48 PM
тАО08-31-2007 12:54 AM - edited тАО09-24-2011 05:48 PM
Re: awk Separating Records
>JRF: means that we have to escape with backslashes making a rather ugly expression.
Yep, awk needs that too.
My script leaves the record separators and joins the lines into a bigger record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 01:37 AM
тАО08-31-2007 01:37 AM
Re: awk Separating Records
>Dennis: My script leaves the record separators and joins the lines into a bigger record.
OK, I missed that requirement, but that's an easy fix:
# perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (@a) {print "NEW RECORD:$b\n\n"}' audit.log
...outputs:
NEW RECORD:1|D|hfoo01||39322|74883|service-call-master2|call-no|1438143|call-sta
tus|D|F3|call-date-last-change|20-AUG-2007|30-AUG-2007
NEW RECORD:1|U|hfoo01||39322|74893|service-call-master2|call-no|1438143|call-use
r-only-num1|0.0000|23158.0000
NEW RECORD:1|I|hfoo01||39322|74893|job-cost-master2|job-code|1438143|jcm-project
-manager|GNOHPH01|HFOO01
NEW RECORD:1|U|hfoo01||39322|74903|service-call-master2|call-no|1438143|call-inv
oice-no||S3106895
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 03:04 AM
тАО08-31-2007 03:04 AM
Re: awk Separating Records
awk '
#prints a newline before printing every line that begins with a 1
#expcept for the first line
/^1/ { if ( NR > 1 )
printf("\n%s",$0);
else
printf("%s",%0);
next;
}
# on every line not beginning with a one
# print the whole line except the first character, but without a carriage return
{printf("%s",substr($0,2));}
'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 09:31 AM
тАО08-31-2007 09:31 AM
Re: awk Separating Records
awk '{if($0~"^1"){if(l) print l;l=$0}else l=l","$0}END{print l}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 06:37 PM
тАО08-31-2007 06:37 PM
Re: awk Separating Records
---------------
BEGIN { getline; save=$0 }
/^1\|[IDU]\|/ {
print save
save=$0"|"
next
}
{
save=save $0"|" # concatenate lines
next
}
END {}
-------------
Voila! I got records with | as the delimiter.
-----------------
% awk -f 1.awk audit.log
1|D|hfoo01||39322|74883|service-call-master2|call-no|143814|3|call-status|D|F|3|
call-date-last-change|20-AUG-2007|30-AUG-2007|
1|U|hfoo01||39322|74893|service-call-master|2|call-no|143814|3|call-user-only-nu
m1|0.0000|23158.0000|
1|I|hfoo01||39322|74893|job-cost-master|2|job-code|143814|3|jcm-project-manager|
GNOHPH01|HFOO01|
-----------
Ah... except for the first record which missed a "|" and the fourth record which is totally missing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 06:41 PM
тАО08-31-2007 06:41 PM
Re: awk Separating Records
----
% awk '{if($0~"^1"){if(l) print l;l=$0}else l=l"|"$0}END{print l}' audit.log
1|D|hfoo01||39322|74883|service-call-master|2|call-no|143814|3|call-status|D|F|3
|call-date-last-change|20-AUG-2007|30-AUG-2007
1|U|hfoo01||39322|74893|service-call-master|2|call-no|143814|3|call-user-only-nu
m1|0.0000|23158.0000
1|I|hfoo01||39322|74893|job-cost-master|2|job-code|143814|3|jcm-project-manager|
GNOHPH01|HFOO01
1|U|hfoo01||39322|74903|service-call-master|2|call-no|143814|3|call-invoice-no||
S3106895|
--------
Yes! All records retrieved!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-31-2007 06:49 PM
тАО08-31-2007 06:49 PM
Re: awk Separating Records
-----------
% perl -0377 -wne 's/\012//g;@a=split(/(?=1\|I\||1\|D\||1\|U\|)/,$_);for $b (>
NEW RECORD:1|D|hfoo01||39322|74883|service-call-master2|call-no|1438143|call-sta
tus|D|F3|call-date-last-change|20-AUG-2007|30-AUG-2007
NEW RECORD:1|U|hfoo01||39322|74893|service-call-master2|call-no|1438143|call-use
r-only-num1|0.0000|23158.0000
NEW RECORD:1|I|hfoo01||39322|74893|job-cost-master2|job-code|1438143|jcm-project
-manager|GNOHPH01|HFOO01
NEW RECORD:1|U|hfoo01||39322|74903|service-call-master2|call-no|1438143|call-inv
oice-no||S3106895