Hi,
I want to search for expressions in a file which include a new-line. Is
this possible with grep or egrep. Or is there another solution with
shell utilities.
thanks in advance
Heiko
Hi,
I want to search for expressions in a file which include a new-line. Is
this possible with grep or egrep. Or is there another solution with
shell utilities.
thanks in advance
Heiko
Neither grep nor egrep have the facility to match over multiple lines.Quote:>Hi,
>I want to search for expressions in a file which include a new-line. Is
>this possible with grep or egrep. Or is there another solution with
>shell utilities.
As usual, with Unix, there are several ways in which you can do this.
If there is a character in your file which occurs with high frequency,
but is not part of your pattern, (space is a good candidate for text files)
then you can use 'tr' to swap between your character and newline, e.g.
e.g. if you are looking for pat\nern
tr ' \n' '\n ' < file | grep 'pat ern'
This is mostly useful if you are just trying to establish the existance of
the match, rather than seeing the rest of the lines.
You can use awk
awk '/^ern/ { if (seenpat) { print saved; print $0 } }
{ if ($0 ~ /pat$/) { saved=$0 ; seenpat=1 } else { seenpat=0 } }'
This works by using a variable seenpat to remember if pat ended the previous
line, and printing the line only if the new line starts with ern and the
previous line ended with pat.
If you have perl installed, you can set it to read in a paragraph at a time
and match over newlines
perl -00ne 'print if (/pat\nern/);'
or you could use sed.
sed -e 'N;/pat\nern/p;D'
This works as follows
The 'N' joins the next line of input onto the 'pattern space'. It includes
a newline as a seperator.
If we see the pattern, then we print out the pattern space.
The 'D' deletes everything up to the first newline from the pattern space,
and causes us to loop back to the beginning.
If you have 'agrep' (approximate grep) installed, then you can set it to
paragraph mode.
I would use perl, because it comes most naturally to mind.
Icarus
1. Find & print lines containing multiple expressions
I have 'inherited' a large text file of collectors names and
abbreviated names of items in their collections. the file format is as
follows:
Owner: Name
dse vvr pod
jup wws ikl
fdt rdf lmo
Owner: Name
aac vgt pod
dse rdf lmo
abc fdt jmk
fgh wws sse
and so on.
I want to be able to search for all instances of, say, wws and then
print the name of the owner. I have limited sed, grep and awk skills
and this has me baffled. Can anyone help please?
TIA
John
3. Display all lines from line which contains some expression
4. Best PC config for linux/crosscompiler development
5. one liner which prints n lines before and m lines after the line found by grep
6. Problem with FX86 and Number Nine FX Vision 330 under RedHat/Alpha 4.2 and RedHat/Alpha 5.0
7. Find a string, delete that line, delete 1 line before it and all lines after it...
8. Help needed with Linux emulation & fortran 95
9. New way to do QOS and limit bandwidth with Linux, new site , new software Howto Included
10. How to include 1 line after the last #include in a bunch of c-files ?
11. Make zImage can't find includes off split-include.c
12. Video Qs: setting more lines, rotating the display.
13. Qs re launching new Linux game.