LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-19-2011, 02:22 PM   #1
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Rep: Reputation: Disabled
Text Processing


I cant find anything that accomplishes what I need. I want to find a 'pattern' and print every line after that until it reaches 'pattern2' until the end of file.

pattern=Trans
pattern2=Port

Quote:
Input File:

Trans

paragraph

Port

paragraph

Trans

paragraph

Port

paragraph

... (Repeats)
Quote:
Output:

Trans

paragraph

Trans

paragraph
Its a file with many occurrences and I only need to extract the paragraph after the string. Thanks!

Last edited by brainAcid; 05-19-2011 at 02:25 PM.
 
Old 05-19-2011, 03:30 PM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
The following awk script reproduces the output you specify from your sample input.

Code:
#! /usr/bin/awk -f

/^Trans$/ { go=1}
/^Port$/  { go=0}

go==1 { print }
 
1 members found this post helpful.
Old 05-19-2011, 03:34 PM   #3
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Sweet!

Thanks!

From IRC:

Quote:
sed -n '/TRANSLATION/,/PURPORT/p' out.txt
 
Old 05-19-2011, 03:47 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by brainAcid View Post
Sweet!

Thanks!

From IRC:
Hi,

in your example you do not want to print the last line of the range, i.e. Port, so you might want to adjust the sed command slightly
Code:
sed -n '/Trans/,/Port/{/Port/!p}' file
 
1 members found this post helpful.
Old 05-20-2011, 04:06 AM   #5
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
Excellent, I learnt something from this too - I have only ever used that sed notation to extract a single block of text and didn't realise that it extracts any number of blocks between matching lines. :-)
 
Old 05-20-2011, 04:47 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,428

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Code:
awk '/Trans|Port/{g=!g}g' file
 
1 members found this post helpful.
Old 05-20-2011, 06:10 AM   #7
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks to everyone!! Very good community here!

I have learned a lot and have accomplished much.

Here is another one:
Quote:
INPUT:

TRANS
paragraph

TRANS
paragraph

...
Quote:
OUTPUT:

Bg1.1
paragraph

Bg1.2
paragraph

...
Now the 'Bg1.1' pattern is in a seperate sorted file.
How do I get each 'TRANS' replaced with 'Bg'?


Thanks!
 
Old 05-20-2011, 06:12 AM   #8
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...-in-sed-62431/

Hmm...
 
Old 05-20-2011, 06:43 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by brainAcid View Post
Not sure where you're going with this but you probably want something like this:
Code:
sed -n '/Trans/,/Port/{/Trans/ {R FILE
d};/Port/!p}' file
Notice, That it is a capital R in contrast to the linked example. It is also important to have line break after the filename FILE because everything that follows the R command will be interpreted as part of the filename.
 
1 members found this post helpful.
Old 05-20-2011, 07:03 AM   #10
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
#!/bin/bash

patternFILE=v.head
workFILE=verses.txt

sed '/TRANSLATION/{R $patternFILE
d}' $workFILE > out.txt
I get output with the TRANS deleted but not replaced.
Using the -n option gives me a blank file.

Quote:
echo "Enter the file name"
read ips

index=0

while read line ; do
sed 's/TRANSLATION/'${line}'/g' verses.txt > out.txt
done < $ips
tried this but it only replaces TRANS with the last line in the file.

?

Last edited by brainAcid; 05-20-2011 at 07:21 AM.
 
Old 05-20-2011, 07:24 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,428

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Code:
awk '/Trans|Port/{g=!g;if(/Trans/)getline < "file2"}g' file
 
1 members found this post helpful.
Old 05-20-2011, 07:29 AM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by brainAcid View Post
I get output with the TRANS deleted but not replaced.
That is because you FILE is supposed to be the filename. Replace FILE with the actual name of your second file.
If you want to store the filename in a variable then you will have to use double-quotes instead of single-quotes to enable expansion. Like this

Code:
#!/bin/bash

patternFILE=v.head
workFILE=verses.txt
sed -n "/Trans/,/Port/{/Trans/ {R $patternFILE
d};/Port/! p}" $workFILE > out.txt

Last edited by crts; 05-20-2011 at 07:36 AM. Reason: fixed typo
 
1 members found this post helpful.
Old 05-20-2011, 07:31 AM   #13
brainAcid
LQ Newbie
 
Registered: May 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
Say the same thing but replacing Trans. Lets forget about Port because I got that section parsed already. Now I need to replace each Trans with each line in file2. Thanks
 
Old 05-20-2011, 07:33 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,428

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Was there something wrong with my solution?
 
Old 05-20-2011, 07:39 AM   #15
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by brainAcid View Post
Say the same thing but replacing Trans.
Which post are you referring to? There was a typo in my previous post. Fixed that and it worked for me.
Also tried grail's solution and it also works fine.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
text processing and cutting eliote Programming 3 10-26-2010 03:28 AM
Text-processing and loops ... skipdashu Linux - Newbie 10 04-30-2009 09:51 PM
text processing, maintaining a log jonhewer Linux - Newbie 9 08-25-2005 02:24 AM
text editingor processing exodist Linux - General 2 11-29-2003 10:24 PM
text processing Gantrep Linux - General 4 02-17-2003 10:37 PM


All times are GMT -5. The time now is 10:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration