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.
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
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.
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.
#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
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 ?
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.