Tuesday, April 28, 2009

Print lines from alternate files - awk


Input files:
$ cat file1
Mr A
Mr B
Mrs C
Mr D

$ cat file2
ID213
ID211
ID218
ID219

Required output:

ID213
Mr A
----
ID211
Mr B
----
ID218
Mrs C
----
ID219
Mr D
----


Awk solution:

$ awk '{print;getline < "file1";print $0 "\n----"}' file2


Python solution for the same:

file1 = open('file1', 'r')
file2 = open('file2', 'r')

for lineA in file1:
print lineA,
print file2.readline(),
print "----"


Related posts:
- Python readline example from my python newbie blog

No comments:

© Jadu Saikia www.UNIXCL.com