Extract between two Exact matched strings.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract between two Exact matched strings.
# 1  
Old 08-02-2016
Hammer & Screwdriver Extract between two Exact matched strings.

more data.txt

Quote:
Start_of_DISK_info:/u
Disk: /u
CURRENT=81
End_of_DISK_info:/u

Start_of_DISK_info:/u/tmp
Disk: /u/tmp
CURRENT=1
End_of_DISK_info:/u/tmp
....
...
i need this exacted from data.txt

Quote:
Start_of_DISK_info:/u
Disk: /u
CURRENT=81
End_of_DISK_info:/u
This is the command i tried

Code:
sed -n "/Start_of_DISK_info:\/u/,/End_of_DISK_info:\/u/p"  data.txt

But, unfortunately it does not do an exact match. Instead, it prints text between both these strings /u & /u/tmp like below.

Quote:
Start_of_DISK_info:/u
Disk: /u
CURRENT=81
End_of_DISK_info:/u

Start_of_DISK_info:/u/tmp
Disk: /u/tmp
CURRENT=1
End_of_DISK_info:/u/tmp
i need this exacted from data.txt

Quote:
Start_of_DISK_info:/u
Disk: /u
CURRENT=81
End_of_DISK_info:/u
Please suggest how can i get exact match.

Last edited by mohtashims; 08-02-2016 at 11:21 AM..
# 2  
Old 08-02-2016
You did not specify an exact end of the string and so you get the pattern each time it is a match.

Try:
Code:
sed -n "/Start_of_DISK_info:\/u$/,/End_of_DISK_info:\/u$/p"  data.txt

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-02-2016
Code:
awk -v r='Start_of_DISK_info:/u' '$1==r' RS='\n\n' data.txt


Last edited by rdrtx1; 08-02-2016 at 06:22 PM.. Reason: changed quotes
This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 08-02-2016
Quote:
Originally Posted by zaxxon
You did not specify an exact end of the string and so you get the pattern each time it is a match.

Try:
Code:
sed -n "/Start_of_DISK_info:\/u$/,/End_of_DISK_info:\/u$/p"  data.txt

i have a variable that stores the path.

Code:
lines=/u/tmp

then i tried this suggestion

Code:
sed -n "/Start_of_DISK_info:\$lines$/,/End_of_DISK_info:\$lines$/p"  data.txt

i get this error

Code:
Unrecognized command: /Start_of_DISK_info:\/u/tmp$/,/End_of_DISK_info:\/u/tmp$/p

Can you tell me a way to fix this on solaris ?

---------- Post updated at 03:41 PM ---------- Previous update was at 03:20 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
awk '$1==r' r="Start_of_DISK_info:/u" RS="\n\n" data.txt

Does not work. Does not print anything.

Last edited by mohtashims; 08-02-2016 at 05:28 PM..
# 5  
Old 08-03-2016
The default search delimiters / / clash with your search string. Take other ones, like \# # (the backslash escapes a sed command)
Code:
sed -n "\#Start_of_DISK_info:${lines}$#,\#End_of_DISK_info:${lines}$#p"  data.txt

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 08-03-2016
Quote:
Originally Posted by MadeInGermany
The default search delimiters / / clash with your search string. Take other ones, like \# # (the backslash escapes a sed command)
Code:
sed -n "\#Start_of_DISK_info:${lines}$#,\#End_of_DISK_info:${lines}$#p"  data.txt

I am getting GRABLED error with the latest suggestion.

Code:
sed: command garbled: \#Start_of_DISK_info:/u/tmp0,\#End_of_DISK_info:/u/tmp0p

Can you suggest a what wrong with this command ?

i changed it to the below to make it work. However, i m not confident about this.

Code:
sed -n "\|Start_of_DISK_info:$lines$|,\|End_of_DISK_info:$lines$|p" data.txt


Last edited by mohtashims; 08-03-2016 at 03:11 AM..
# 7  
Old 08-03-2016
Yes that works. I forgot that the shell sees the $# as a special variable and substitutes it.
This User Gave Thanks to MadeInGermany 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

Issue with sed command does not replace exact string matched

I have a file change.sed more change.sed I fire the below command inorder to replace "190.169.11.15" with "10.4.112.240" in proxy.logsed -f change.sed proxy.log proxy.log has the below entry more proxy.log The command replaces both 190.169.11.15 & 190.169.11.155 as below: I am expecting... (17 Replies)
Discussion started by: mohtashims
17 Replies

2. UNIX for Beginners Questions & Answers

Count exact matched words

hi , i have a file test.dat which contains following data. test.dat XY|abc@xyz.com XY|abc@xyz.com ST|abc@xyz.com ST|abc@xyz.com ST|XYZ@abc.com FK|abc@xyz.com FK|STG@xyz.com FK|abc@xyz.com FK|FKG@xyz.com i want to know the count of XY,ST,FK. i.e XY = 2 , ST = 3 , FK = 4 I am... (4 Replies)
Discussion started by: itzkashi
4 Replies

3. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

6. Shell Programming and Scripting

How to extract exact strings in shell scripting

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

7. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies

8. Shell Programming and Scripting

Grepping for Exact Strings

ok, apparently this is a very difficult question to answer based on my searches on google that came up fruitless. what i want to do is grep through a file for words that match a specified string. but the thing is, i keep getting all words in the file that have the string in them. say for... (27 Replies)
Discussion started by: SkySmart
27 Replies

9. Shell Programming and Scripting

sed replaces all matched strings!!!

hi sed -e '/<group>/!s/group\(.*\)/group\: files compat/' /etc/nsswitch.conf returns group: files compat netgroup: files compat How to prevent changing netgroup entry?? thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies
Login or Register to Ask a Question