Grep string in a file and paste next line in a specific way

Hello,
I know there are many questions and replies regarding grep command.
What I would like to do is a bit different.

File A:

hello world welcome to my page
this is my test site
how are you
I am fine, thank you
where have you been
I was in hospital
really hope you are fine now
Thanks, all black days are away now
What about your family
Thank you so much, All good
Sounds nice
Cheers
You too
Cheers2

FileB:

hello world welcome to my page
how are you
where have you been
really hope you are fine now
What about your family
Sounds nice
You too
grep -A1 'world' FileA | grep -A1 'page'

It will grep next one of the line containing world and page words and then it will paste it to FileB after matching line.

Expected output should be:

hello world welcome to my page
this is my test site
how are you
where have you been
really hope you are fine now
What about your family
Sounds nice

Really appreciated if you could lead me how to solve this.

Thanks
Boris

Your specification is a bit vague. Find ALL lines with "world" and "page", even if there are more than one? Where to insert them (we can only guess: after a (ALL?) line(s) with "world" and "page" in them)?

Anyhow, all that can't be done with grep . Try (if you've got GNU sed ):

sed "/world.*page/a $(sed -n '/world/ {n;p;q;}' file1)" file2

For other sed s, try

sed "/world.*page/a\
$(sed -n '/world/ {n;p;q;}' file1)" file2
2 Likes

Hello Rudic,
Thanks for your reply.
It works nice if there is no more than one match.
Regarding the second code, I could not get the difference between the first one.

Boris

Refine your spec if your requirements are not fulfilled. BTW, it's better to have a complete, detailed spec in the first place, so people can work off it.
Most non-GNU sed s NEED the line break for text to be inserted. See man sed of your version.

Hello Again Rudic,
Regarding your notice, I have just updated the scenario as I missed some cases as you pointed out.

FileA

#PRESEPERATOR London 
information1 
#PRESEPERATOR Manchhhhester
information2 
#PRESEPERATOR Bristooool
information3 
#PRESEPERATOR Birminghaaam
information4 
#PRESEPERATOR Leeeeeeds
information5 
#PRESEPERATOR Liverpooooool 
information6 
#PRESEPERATOR Sheffielddddddd
information7 
#PRESEPERATOR Nottinghaaaam
information8 
#PRESEPERATOR Newcastleeeee
information9 
#PRESEPERATOR London UK GB +44
information55
#PRESEPERATOR Glasgowwwwww
information10 
#PRESEPERATOR Southamptonnn
information11
#PRESEPERATOR Plymouth
information45

FileB

#PRESEPERATOR London 
#PRESEPERATOR Manchester
#PRESEPERATOR Bristol
#PRESEPERATOR Birmingham
#PRESEPERATOR Leeds
#PRESEPERATOR Liverpool 
#PRESEPERATOR Sheffield
#PRESEPERATOR Nottingham
#PRESEPERATOR Newcastle
#PRESEPERATOR London UK GB +44
#PRESEPERATOR Glasgow
#PRESEPERATOR Southampton
#PRESEPERATOR Portsmouth
#PRESEPERATOR Conventry
#PRESEPERATOR Norwich
#PRESEPERATOR Oxford
#PRESEPERATOR Belfast 
#PRESEPERATOR Plymouth

What I wish to do is:

Excluding the first column in each row in FileA , grep each column value
For example if the line has three columns in total, get 2nd column (c2) and third column (c3).
Then search c2 and c3 in FileB
When both c2 and c3 exists on FileB , cut the next line in FileA and paste into next line in FileB . Then print out only matching lines, ommit not found lines in FileB.

Expected output:

#PRESEPERATOR London 
information1 
#PRESEPERATOR London UK GB +44
information55
#PRESEPERATOR Plymouth
information45

Thanks for your patience
Boris

Not doable with sed . Try

awk '
NR == FNR       {getline TMP
                 $1 = ""
                 T[$0] = TMP
                 next
                }
                {TMP = $0
                 sub ($1, "", TMP)
                }
TMP in T        {print
                 print T[TMP]
                }
 ' file[12]
#PRESEPERATOR London
information1 
#PRESEPERATOR London UK GB +44
information55
#PRESEPERATOR Plymouth
information45

Hello Rudic,
I could not get what you implied.
Should I enter the info #PRE**** section in the code?
Can't we do it by read column nr and read 2nd and following columns in each line ?

I am sorry for the pain
Boris

What in the solution proposed doesn't satisfy your request?

Dear Rudic,
What does file12 stand for in this case?
Here is my console:

root@vps112233:~/test# awk '
> NR == FNR       {getline TMP
>                  $1 = ""
>                  T[$0] = TMP
>                  next
>                 }
>                 {TMP = $0
>                  sub ($1, "", TMP)
>                 }
> TMP in T        {print
>                  print T[TMP]
>                 }
>  ' FileA FileB

It gives empty result

Thanks
Boris

It is not file12 , but file[12] , which will be expanded by the shell to file1 file2 . Your FileA FileB should do.
Applied to your sample in post#1, it gives the result shown in post#6. Did you use exactly the sample files from post#1? Check for spurious spaces in either, or other peculiarities.

1 Like

Dear Rudic,
Have just got the meaning of [] notation now,
Output is the same as I expected. Thank you so much! You are a star!

Kind regards
Boris