Print text between 2 identical strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print text between 2 identical strings
# 1  
Old 10-20-2015
Wrench Print text between 2 identical strings

hey,
i m having a hard time trying to print only the first occurrence between 2 idenicale strings.

for the following output:
Code:
please
help
me im a 
noob
please
im a noob
help me
noob
please
help
me im a 
noob
please
im a noob
help me
noob

i m trying to get only the first occurrence between "noob" and "noob" like so:

Code:
noob
please
im a noob

thats what i got so far:

Code:
sed -e '1,/noob/d' -e '/noob/,$d' file.txt

but it returns only:
Code:
please
im a

but i want it to also print the "noob" string.

any help will be great
Thanks!!!
# 2  
Old 10-20-2015
Hello boaz733,

Could you please try following and let me know if this helps you.
Code:
awk '/noob/{A++;if(A==1 || A==2){print};next};{if(A==1){print}}'  Input_file

Output will be as follows.
Code:
noob
please
im a noob

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-20-2015
it's perfect - thanks!
# 4  
Old 10-20-2015
Try
Code:
sed -n '/noob/{:L;N; /noob.*noob/bK; bL; :K;p;q}' file
noob
please
im a noob

---------- Post updated at 15:04 ---------- Previous update was at 14:58 ----------

awk:
Code:
awk '/noob/ {print; if (L) exit; L=1; next} L'  file2
noob
please
im a noob

These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print text between 2 strings for the entire file

hey guys, for the following output: starting open open close close starting close starting open close close starting open open close open (2 Replies)
Discussion started by: boaz733
2 Replies

2. Ubuntu

Merging strings that have identical rownames in a dataframe

Hi I have a data frame with repeated names in column 1, and different descriptors in column 2. I want to merge/cat strings that have same entry in column 1 into one row with any separator. Example for input: Cvel_1 KOG0155 Cvel_1 KOG0306 Cvel_1 KOG3259 Cvel_1 ... (4 Replies)
Discussion started by: Alyaa
4 Replies

3. UNIX for Dummies Questions & Answers

How to print the text between two strings in unix.

Hi Team, Would you please help me for the below scenario. I want to print the text between "PREF:" AND "AVAIL:" in the below example. For example:- TEST_TAF PREF: RAC1 RAC2 RAC3 ...... AVAIL: RAC4 Output will be :-RAC1,RAC2,RAC3............. Thanks in Advance Shoan ... (5 Replies)
Discussion started by: shoan
5 Replies

4. Shell Programming and Scripting

[Help me!] Print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

5. Shell Programming and Scripting

[Help me!] print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

6. Programming

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

7. Shell Programming and Scripting

finding and removing block of identical strings

i have a problem in finding block of identical strings...i solved the problem in finding consecutive identical words and now i want to expand the code in order to find and remove consecutive identical block of strings... for example the awk code removing consecutive identical word is:... (2 Replies)
Discussion started by: cocostaec
2 Replies

8. Shell Programming and Scripting

Using Bash/Sed to delete between identical strings

Hi. I'm hoping that someone can help me with a bash script to delete a block of lines from a file. What I want to do is delete every line between two stings that are the same, including the line the first string is on but not the second. (Marked lines to match with !) For example if I... (2 Replies)
Discussion started by: Zykr
2 Replies

9. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

10. Shell Programming and Scripting

replace 2 identical strings on different lines

I am looking to replace two or more strings on different lines using sed, but not with the same variable. IE # cat xxx.file <abc> abc def ghi abc def ghi abc def ghi currently I can only change each line with the same pattern: # sed -e '/<abc>/!s/abc\(.*\)/jkl mno/' xxx.file abc jkl mno... (3 Replies)
Discussion started by: prkfriryce
3 Replies
Login or Register to Ask a Question