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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep string in a file and paste next line in a specific way
# 1  
Old 08-20-2018
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:
Code:
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:
Code:
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

Code:
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:
Code:
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




Moderator's Comments:
Mod Comment Please use CODE (not QUOTE!) tags as required by forum rules!

Last edited by RudiC; 08-20-2018 at 06:55 PM.. Reason: Changed QUOTE to CODE tags.
# 2  
Old 08-20-2018
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):


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

For other seds, try

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

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 08-21-2018
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
# 4  
Old 08-21-2018
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 seds NEED the line break for text to be inserted. See man sed of your version.
# 5  
Old 08-22-2018
Hello Again Rudic,
Regarding your notice, I have just updated the scenario as I missed some cases as you pointed out.

FileA
Code:
#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
Code:
#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:
Code:
#PRESEPERATOR London 
information1 
#PRESEPERATOR London UK GB +44
information55
#PRESEPERATOR Plymouth
information45


Thanks for your patience
Boris

Last edited by baris35; 08-22-2018 at 06:15 PM..
# 6  
Old 08-22-2018
Not doable with sed. Try
Code:
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


Last edited by RudiC; 08-23-2018 at 03:45 AM.. Reason: corrected typo in the result
# 7  
Old 08-22-2018
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

2. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

3. Shell Programming and Scripting

How to grep an empty line in a specific column of a file?

Suppose i have the following data : cat file.txt 12431,123334,55353,546646,14342234,4646,35234 123123,3535,123434,132535,1234134,13535,123534 123213,545465,23434,45646,2342345,4656,31243 2355425,2134324,53425,342,35235,23434,234535 3423424,234234,65465,,2344,35436,234524,234... (7 Replies)
Discussion started by: Ravi Tej
7 Replies

4. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Programming

How to grep the specific string or user's list from the file

I have a file on UNIX system from where I want to grep the list of all users associated to the particular repository.If the user's list is in single line then I fetch all list but if it is in two separate lines it doesn't.I use the below command a=KESTREL-DEV;b=users;cat access_file|grep... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

6. Shell Programming and Scripting

Remove a specific line from grep output string

Dear All I want to search string "1000" from input file and if it found i want remove line that contain 1000 and also remove 3 line above it and 2 line below it. INPUT FILE: BHAT-D 2 aaa ID CODE GS UPDATE MODE LANG MCO MCL NUMPAGES 50 ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

7. Shell Programming and Scripting

Deleting a line from a file based on one specific string instance?

Hello! I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs. Let's assume I have a file composed of thousands of lines... (4 Replies)
Discussion started by: Black Sun
4 Replies

8. Shell Programming and Scripting

grep a specific line from a file

Is there a way to grep only one line from a file listing name of files? If I use head -1 it's ok(the first line only) If I use head -2 it's not good because I have already checked the first line. I'd like something like this: while test $i -le $max do grep "$3" `head -$i temp.txt` >... (4 Replies)
Discussion started by: Max89
4 Replies

9. Shell Programming and Scripting

grep on specific line of the file

Hi, how can we search for a word (with case ignore )on specific line numbers ex: Awk /^regEX/ with condition on first line or second line Awk/^ regex1/ on line 1 Awk /^regEX2/ on line 3 thanks in advance (4 Replies)
Discussion started by: rider29
4 Replies

10. Shell Programming and Scripting

copy and paste a specific line

Hi I am having some trouble cut and paste a file based on the content of another file. I have a file called draft. I need to cut and paste its content to another file based on the content of a file called proc.txt The content of proc.txt is like the following:... (7 Replies)
Discussion started by: tiger99
7 Replies
Login or Register to Ask a Question