2

While looking at another question, I came across a problem I couldn't solve generally for awk, where if an awk program takes two files (i.e. to read entries from the first into an array to compare with, as is often the case) what's the best way to deal with changing RS between files?

Given the following files:

~$> cat awktest1.txt 
111 112 113 114|121 122 123 124|131 132 133 134|141 142 143 144
 ~$> cat awktest2.txt 
211 212 213 214

221 222 223 224

231 232 233 234

241 242 243 244

if I wanted to run the following, as a basic example:

#! /usr/bin/awk -f

# awktest.awk file1 file2
# cant swap RS between files

BEGIN { RS="|" }

NR>ONR && ONR==1 { RS="" }
{ print $1 "." $2 "." $3 "." $4 }

# will work with with, but this is GNU only.
# ENDFILE { RS="" }
END { print "\nfinal $0: \n" $0 }

then as RS is changing after the first record of the second file has been read, so in this (admittedly contrived example) the output is:

~$>./awktest.awk awktest1.txt awktest2.txt 
111.112.113.114
121.122.123.124
131.132.133.134
141.142.143.144
211.212.213.214

final $0:
211 212 213 214

221 222 223 224

231 232 233 234

241 242 243 244

with all of the second file being read as a single record, instead of 4. Clearly with this example, it'd be simple to fix the issue before awk, but is there any way actually in awk?

8

Arguments like file names and assignments can be intermixed so you could run:

awk '{ print $1 "." $2 "." $3 "." $4 }' RS='|' file1 RS='' file2

as awk will process them in order so you can set a different RS for each file.


Note that awk might choke on filenames containing the = character.
Here's how to work around that..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.