awk - set RS from the input file

Viewed 80

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?

3 Answers

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

you can kinda cheat with a function like this :

  • caveat : this gets you the n-th byte, not necessarily n-th character

.

 {m,g}awk 'BEGIN {
     1      _=__=___=_<_
     1      RS = getNthByte(ARGV[ARGC-!_], 9)
          }
 13929783  {
 13929783   ___+=length($_)
 13929783    __+=NF
          }
    function getNthByte(_,____,__,___)
    {
     1      gsub("\47", "&\\\\&&", _)
     1      ___ = RS
     1       RS = "^$"
     1      (__ = "ghead -c "(____)" \47"(_)"\47 | gtail -c 1 ") | getline _
     1      close(__)
     1      RS = ___
     1      return _
    
    } END { print _=NR,__,___+_ }' "${file}"
13929783 20069465 160950672
Related