1

I want to stimulate use of sed:

sed  '3r awk.scr' awk.script

with awk.scr:

a
b
c 
d 
e 
f

and awk.script as:

hello there is
hello i'am there is
hello sdfdf
dfdfdf aads
23213 3 434

Using awk here:

awk 'BEGIN {while((getline gf < "awk.script") > 0) {print gf; if(++i > 2) break;} {while((getline bf < "awk.scr")> 0 ) { print bf}}}'

But its too complex is there a easy way out.

Desired output:

hello there is
hello i'am there is
hello sdfdf
a
b
c
d
e
f
  • Are you sure the desired output is what you got when you run sed '2r awk.scr' awk.script? It should be 3r instead of 2r. – cuonglm Dec 30 '15 at 10:19
3

An easier way can be:

awk 'FNR == 3 {print;while(getline < "awk.scr") print; next};1' awk.script

or:

awk 'FNR == 4 {while(getline < "awk.scr") print};1' awk.script

With this approach, you only need to care about the awk.scr, awk will process awk.script for you.

| improve this answer | |
  • thanks for response. Love the neat code.Can you explain are you using flag with 1 option. Why does it require next when there already is a while loop? – asad Jan 1 '16 at 12:49
  • The 1 is a true expression, cause awk print $0 by default. The next there to stop awk executing 1 expression after the while loop, otherwise, you will print the third line two times. I updated with another solution without the next. – cuonglm Jan 1 '16 at 12:56
  • thanks for explanation, i tested the original cmd without next and with it and it had no difference of the output. There was no printing of the third line x3 times. – asad Jan 1 '16 at 13:02
  • Even, then It has no effect on o/p. – asad Jan 1 '16 at 13:37
  • It's actually a blank line, $0 set by getline was empty after print. – cuonglm Jan 1 '16 at 14:01

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.