Line number based manipulation

Hi
I have a case where I am grabbing patterns and subsequent lines using

sed -n -e '/regex/{$!N;p;}'

This returns just the regex line when it is the last line of my file.

Now I may have even number of lines in some cases (regex never at end) and odd in very rare cases.

If the line count is odd, then I want to append to this a line from another file.

How do I do this? I dont know how to refer to another file within a awk file processing on someother file.

Please give an example of :

  • what input you have
  • the name and content of the file from which you want to extract the line to be merged with your output in the odd case
  • what output you expect.

Hi
I am giving you the simplest example-
After I get regex and the next line, I further get only the columns that I am interested in. So it is something like this-

10
20
45
47 

(this is the case of lines being present after regex), if not the last lines is not present, ie

10 
20 
45

If I find this has odd number of lines- I want to take input from another file which is input.txt with content
90 xxxxxxxxxxx (I will take only the first column from this) and merge as the fourth line of the file.

is this example ok? lemme know if something is unclear

This would add the content of input.txt as a last line, if the line count is odd...

sed '/regex/!d;$!{N;p;d;};r input.txt' infile

HI

Thanks a lot. that seems so elegant and works perfectly!
If possible could you give hints on how it works..

Thanks again!

Thanks!