Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi record merge

Status
Not open for further replies.

hfeldma1

Programmer
Jul 21, 2004
6
CA
Hi

I'm looking for an awk script that can merge the following two records into 1 record. Can anyone help?
StaticAddrInfo: s1-1-1-1@xx.net@0:0:0:0:0:0@1.1.
1.1

The key is that the second record does not contain a ':'
 
Something like this ?
awk '{buf=buf""$0;if($0!~/:/){print buf;buf="")}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Is there a way to do only the merge if it starts with StaticAddrInfo: ? For lines that do not start with StaticAddrInfo: just print it as is.

I've tried the following but it's not working?

awk '{buf=buf""$0;if($0~/StaticAddrInfo:/){if($0!~/:/){print buf;buf=""}}}'
 
Can you please post more consistent input example and expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Input
HsiaServiceOption: Server-2-IP@0@0@2@2@yes
HsiaServicePoint: Region 650
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@21
6.232.87.259
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@21
6.232.87.260
modifiersName: uid=superadmin,dc=.
modifyTimestamp: 20031203063727Z

Expected output
HsiaServiceOption: Server-2-IP@0@0@2@2@yes
HsiaServicePoint: Region 650
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@216.232.87.259
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@216.232.87.260
modifiersName: uid=superadmin,dc=.
modifyTimestamp: 20031203063727Z
 
Something like this ?
awk '/HsiaStaticAddrInfo:/{printf $0;getline}1' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Close, it doesn't handle the following condition.

HsiaServiceOption: Server-2-IP@0@0@2@2@yes
HsiaServicePoint: Region 650
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@2
16.232.87.259
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@2
16.232.87.260
HsiaStaticAddrInfo: s1-1-1-1@xx.net@0:B0:D0:E7:2E:14@216.232.87.270
modifiersName: uid=superadmin,dc=.
modifyTimestamp: 20031203063727Z
 
Something like this ?
awk '/HsiaStaticAddrInfo:/{printf $0;getline;if($0~/:/)printf "\n"}1' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Not quite.

First the 1 cause an error. If I remove it it runs.
Second it only outputs the lines that contian HsiaStaticAddrInfo

I need the other sample lines outputed as well unmodified.
 
First the 1 cause an error
Even with nawk ?
Its a shorthand for {print}
So here the script for old awk:
awk '
/HsiaStaticAddrInfo:/{
printf $0;getline;if($0~/:/)printf "\n"
}
{ print }
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
nawk worked. Just one bug that I've discovered in testing. The following input

modifyTimestamp: 20040719990632Z
modifiersName: uid=superadmin,dc=.
HsiaStaticAddrInfo: s999-232-127-159@xxx.net@0:0:0:0:0:0@999.23
2.127.159
HsiaStaticAddrInfo: s999-232-127-160@xxx.net@0:0:0:0:0:0@999.23
2.127.160
HsiaStaticAddrInfo: s999-232-127-161@xxx.net@0:0:0:0:0:0@999.23
2.127.161
HsiaStaticAddrInfo: s999-232-127-162@xxx.net@0:0:0:0:0:0@999.23
2.127.162
HsiaStaticAddrInfo: s999-232-127-158@foo.bar@1:2:3:4:5:6@999.232.127.158
HsiaStaticAddrInfo: s999-232-127-185@xxx.net@0:26:54:13:DB:5C@9
16.232.127.185
HsiaStaticAddrInfo: s999-232-127-183@xxx.net@0:D:87:39:AA:7A@91
6.232.127.183
HsiaStaticAddrInfo: s999-232-123-239@xxx.net@0:4:E2:2:CB:5A@999
.232.123.239
bctacHsiaServicePoint: Region 600
bctacHsiaServiceOption: SOHO@0@0@5@5@yes
createTimestamp: 19990929125757Z

generated the following output. Line with 999-232-127-185 is not merged.

modifyTimestamp: 20040712160632Z
modifiersName: uid=superadmin,dc=.
HsiaStaticAddrInfo: s999-232-127-159@xxx.net@0:0:0:0:0:0@999.23 2.127.159
HsiaStaticAddrInfo: s999-232-127-160@xxx.net@0:0:0:0:0:0@999.23 2.127.160
HsiaStaticAddrInfo: s999-232-127-161@xxx.net@0:0:0:0:0:0@999.23 2.127.161
HsiaStaticAddrInfo: s999-232-127-162@xxx.net@0:0:0:0:0:0@999.23 2.127.162
HsiaStaticAddrInfo: s999-232-127-158@foo.bar@1:2:3:4:5:6@999.232.127.158
HsiaStaticAddrInfo: s999-232-127-185@xxx.net@0:26:54:13:DB:5C@9
16.232.127.185
HsiaStaticAddrInfo: s999-232-127-183@xxx.net@0:D:87:39:AA:7A@91 6.232.127.183
HsiaStaticAddrInfo: s999-232-123-239@xxx.net@0:4:E2:2:CB:5A@999 .232.123.239
bctacHsiaServicePoint: Region 600
bctacHsiaServiceOption: SOHO@0@0@5@5@yes
createTimestamp: 19990929125757Z
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top