Squares in saved code

can you help

i am merging 2 files together and saving to a third file with awk
and its working with this code

awk 'OFS="";NR==FNR{a[FNR]=$0;next} {print a[FNR],"\n","\b",$0}' file1 file2 > file3

the problem is in file3 when its saved
i get a small square at the start of every 2nd line (see picture)

i need to get rid of the squares
whats wrong with the code ?

thanks

File 1

AAAA
BBBB
CCCC

File 2

XXXX
YYYY
ZZZZ

File 3

AAAA
XXXX
BBBB
YYYY
CCCC
ZZZZ

Why are you printing a backspace character ( \b ) ? Just leave it out.

--
As a matter of efficiency, set OFS="" in the BEGIN section...
In fact, you do not need it at all if your use print a[FNR] "\n" $0

1 Like

@Scrutinizer

i have removed \b from the code

awk 'OFS="";NR==FNR{a[FNR]=$0;next} {print a[FNR],"\n",$0}' file1 file2 > file3

and the squares have gone

thank you

This is exactly what paste is for:

paste -d"\n" file[12]
AAAA
XXXX
BBBB
YYYY
CCCC
ZZZZ
1 Like

If you go with awk instead of RudiC's suggestion of paste you can still simplify your code (as Scrutinizer suggested) and get exactly the same output:

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR] "\n" $0}' file1 file2 > file3

(Note that the commas in your print statement are the only things that use the value of OFS in your script.)

1 Like

Or

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR]} 1' file1 file2 > file3
1 Like

Or:

awk '1;getline<f>0' f=file2 file1 > file3
2 Likes