Using quotes in awk

Hello, i had a problem running a script , and after investigation found its all to do with the quotes:

cat file1

line1
val1
val2

line2
val1
val2

line3
val1
val2
 awk 'BEGIN {RS="\n\n"; FS="\n";} {print $1 $2}' file1 

This gives me the wrong output:

line1
val1
val2

line2
val1
val2

line3
val1
val2

Only after experimenting with the quotes did i get what i required:

awk 'BEGIN {RS='\n\n'; FS="\n";} {print $1 $2}' file1
line1val1
line2val1
line3val1

Im using AIX/KSH.

All the documentation i have found says use double quotes and in fact when i tried using a single character as a RS it gave give me a syntax error if i used single quotes, so im confused as to why \n is different.Is this specific to ksh or am i doing something wrong?

Many thanks

awk '{print $1 $2}' RS="\n\n" FS="\n" file1
awk '{print $1 $2}' RS='\n\n' FS='\n' file1
awk '{y=getline x; if (x && y) print $1 x}' file1
awk '!NF {l=l RS; c=0} NF && c++<2 {l=l $0} END {print l}' file1
awk '{print $1 $2}' RS="\n\n" file1  

and

awk '{print $1 $2}' RS='\n\n' file1

both give wrong output:

line1
val1
val2

line2
val1
val2

line3
val1
val2

Try using nawk instead of awk on AiX.

i tried nawk this gives the same output.

Is this problem exclusive to AIX?

It is not restricted to AIX. The standards explicitly state that the input Record Separator is the 1st character or the string assigned to RS and the results are unspecified if the string assigned to RS contains more than one character. Some implementations of awk allow using an ERE in RS (which is required by the standards for FS , but not for RS ). The behavior you are seeing on AIX is common for most implementations of awk on UNIX and BSD systems. Most, if not all, Linux systems support the extension that allows RS to be set to an ERE.

With a standards-conforming awk :

awk '
c == 0 {
	f1 = $0
	c = 1
	next
}
c == 1 {
	print f1 $0
	c = 2
}
NF == 0 {
	c = 0
}' file1

should do what you want.

If someone wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

1 Like