Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate.

This is in reference to this thread -> Extract between two Exact matched strings.

I have data.txt as below.

I have a variable as below.

lines=/u

I wish to EXACT match and extract data From Start_of_DISK_info:$lines to End_of_DISK_info:$lines as shown below.

I tried this as a solution

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

But, it is matching both /u and /u/tmp thus extracting the whole data.txt.

So, it is not doing an exact match which is what i was looking for.

Can you tweak this command to get the desired results?

sed is often a difficult choice for anything stretching across multiple lines. awk has a special setting, RS="", to read entire blocks of text separated by blank lines at once.

$ awk 'index($0, "Start_of_DISK_info:"S"\n")==1' RS="" ORS="\n\n" S="/u" data

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

$

You'd have S="$lines"

First, you need to define what "exact match" means for you: actually it matched the exact phrase you were searching for - the line contained just something else besides of what you matched. What you are looking for is not a "exact match" but an "exact match with nothing else to follow", yes?

Yes, i can and so can you. Reading the above should make it obvious: anchor the regexp to the end of line.

I hope that helps.

bakunin

Believe me it does not return any output. I am on

uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Show your actual input, please. Attach it if necessary.

Also, try nawk.

You can use sed to process more than one line with the command N to append the next line to the "pattern" space (usual area that can be searched or modified) and the 2nd line will be separated from the 1st by a newline "\n". If you use this you might need to put your sed commands in a separate file rather than the command line.

There is no problem, the sed in post#1 works i.e. returns the first section from the given data.txt!
Maybe it is in WinDOS\r format?

The problem i dont get any output is becoz if there is any newline between the start string and end string in data.txt your suggestion will return not give desired result as expected.

Somehow we can disregard the newlines between the search strings ?

---------- Post updated at 06:26 PM ---------- Previous update was at 06:21 PM ----------

Please consider newlines between the search strings.

I dont know what is and how to confirm if i have WinDOS\r format.

The /pat1/,/pat2/ or \|pat1|,\|pat2|
is a range match, from the line where pat1 matches up to the line where pat2 matches. The p command prints the range.
Because your pat1 and pat2 end with a $ there must not be a trailing character.
Open the file with vi (not vim). Is there a trailing ^M or space character at the end of the line?
Tip: a trailing space character becomes visible in vi after a :set list (the line ends are shown as $ ). Switch back to the normal view mode with :set nolist

I am pasting everything here so you can try the same and see it failing. By the way the issue was the format of data.txt

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

Disk: /u
CURRENT=81
Disk Growth Weekly=
Disk Growth Monthly=
End_of_DISK_info: /u        ----> there is a space between : and /u causing the problem.
Start_of_DISK_info:/u/tmp

Disk: /u/tmp
CURRENT=1
Disk Growth Weekly=
Disk Growth Monthly=
End_of_DISK_info:/u/tmp

And now ....

cat -ev data.txt
$
Start_of_DISK_info:/u$
$
Disk: /u$
CURRENT=81$
Disk Growth Weekly=$
Disk Growth Monthly=$
End_of_DISK_info: /u$
Start_of_DISK_info:/u/tmp$
$
Disk: /u/tmp$
CURRENT=1$
Disk Growth Weekly=$
Disk Growth Monthly=$
End_of_DISK_info:/u/tmp$

Thank you for all the help.

Allow the spaces with

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