-2

I need to insert a blank line after two lines in a file that I did with

cat rat     hat
pot NOT     lot
big big     big
sit sit     sit
awk '{print};(NR%2==0){print ""}'
cat rat     hat
pot NOT     lot

big big     big
sit sit     sit

Actually my main target is to insert a new word after every two lines and I can't solve.

How can I do as I have <= 6000 names in another file in column three and even in this column few have just .(dot) in it.

Second I don't want the blank line after last line.

1

This seems perfect, to insert fixed WORD every two lines

awk '{print}
     (NR%2==0){printf "WORD\n"}'

not to insert on last line, i would suggest

awk 'NR>2 && (NR%2==1) {printf "WORD\n"}
 { print ; }' 

number of column in line doesn't matter.

However, just guessing, it is not a fixed word ? a word from the line ? the last one ?

awk 'NR>2 && (NR%2==1) {printf "%s\n",last}
 { print ; last=$NF ; }' 

this will print, the last word every other line.

Replace $NF by $3 to get the third.

Creating a program is not only code, but being able to describe to others (and oneself) what is to be achieved.

0

Using awk we can print new word when linenumber is even

awk '{{print $0}if(NR%2==0)print "word"}' 
  • This adds nothing  to the existing answer. – G-Man Mar 9 at 6:29

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.