2

I have two files, file1 and file2. They both have X lines. Some lines in file2 are empty(they match ^$). If line I is empty in file2, I want to remove line I from both files. Example:

File1:

ad
dada

da
dad


a

File2:

dada


da
dada
dada
da
(empty line)

Resulting file1:

ad
da
dad


(empty line)

Resulting file2:

Dada
da
dada
dada
da
3

Can use getline in this case:

$ awk '{ getline ln < "File1"; if(!/^$/){print ln > "tmp1"; print > "tmp2"} }' File2
$ cat tmp1
ad
da
dad


$ cat tmp2
dada
da
dada
dada
da

Then mv the output files for inplace editing if needed

1
  • 1
    This is simple(one-liner), readable and flexible approach. I like it, and used it. Thanks! Nov 19 '16 at 19:54

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.