I've got a file where the first few bites define some of the attributes of the file.
The 9th bite is the record separator. So the records are not separated by \n but by whatever is on the 9th bite.
I need to read this file, set RS and then read the file "again" but now separated by this new record separator.
Input file (here the record separator is '):
UNA:+,? 'UNB+UNOC:3+4042805000102:14+4016001000655:14+201231:0206+EC33218279A++TL'UNH+1+MSCONS:D:04B:UN:2.3'BGM+7+EC33218279A-1+9'DTM+137:202012310206:203'RFF+Z13:13018'NAD+MS+4042805000102::9'NAD+MR+4016001000655::9'UNS+D'NAD+DP'LOC+172+DE00108108359V0000000000000088446'DTM+163:202012300000?+01:303
awk program would be something like:
{ RS=substr($0, 9, 1) }
{ do magic }
Is it somehow possible?
If you never expect the 9th char to also appear in the first 8 chars, or if it does you don't want it treated as an RS in that context, then you could use this 1-pass approach using GNU awk for multi-char RS and RT:
$ awk -v RS='.{9}' 'NR==1{$0=substr(RT,1,8); RS=substr(RT,9,1)} 1' file
UNA:+,?
UNB+UNOC:3+4042805000102:14+4016001000655:14+201231:0206+EC33218279A++TL
UNH+1+MSCONS:D:04B:UN:2.3
BGM+7+EC33218279A-1+9
DTM+137:202012310206:203
RFF+Z13:13018
NAD+MS+4042805000102::9
NAD+MR+4016001000655::9
UNS+D
NAD+DP
LOC+172+DE00108108359V0000000000000088446
DTM+163:202012300000?+01:303
A 2 pass gnu awk would do that as we need to set multi-char RS:
awk -v RS='.{9}' 'FNR == NR {RS=substr(RT,9,1); nextfile} 1' file file
UNA:+,?
UNB+UNOC:3+4042805000102:14+4016001000655:14+201231:0206+EC33218279A++TL
UNH+1+MSCONS:D:04B:UN:2.3
BGM+7+EC33218279A-1+9
DTM+137:202012310206:203
RFF+Z13:13018
NAD+MS+4042805000102::9
NAD+MR+4016001000655::9
UNS+D
NAD+DP
LOC+172+DE00108108359V0000000000000088446
DTM+163:202012300000?+01:303
Thanks to Ed for suggesting this any version awk solution:
awk 'NR==FNR {
if (!f) {
rec = rec sep $0
sep = RS
if (length(rec) >= 9) {
RS = substr(rec,9,1)
f = 1
}
}
next
}
1' file file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With