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.
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.
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.
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$