RegExp: From first occurrance to last (at line start)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting RegExp: From first occurrance to last (at line start)
# 1  
Old 01-07-2008
RegExp: From first occurrance to last (at line start)

So I have a log that contains something like this:
Quote:
preparing localhost0...
[some number of lines of junk]
localhost0: ls /
bin boot dev etc home lib lib64 lost+found misc mnt net opt proc root sbin selinux srv sys tmp tools usr var
localhost0: exit
[some number of lines of junk]
What I want is to get the first occurrence of "^localhost0" (at line start) and then everything else up to the last occurrence of "^localhost0" (at line start)

Ideally I don't even care about the 2 "localhost0" lines either. All I really care about is the information stored between the command and the exit call.

I cannot seem to think of a way to do this with grep... aka

Quote:
Grab the first line starting with "localhost0", plus all lines that follow, stopping at the last occurrence of "localhost0" (that is at the start of a line) prior to file end.
Any help?
# 2  
Old 01-07-2008
I can think of two approaches:
Store the data to display in memory while you figure out what is 'in' and what's 'out'.
Run through the file a couple of times.

As I don't know how big the file is, we'll go with the multiple passes solution - it's ugly but it has a predictable memory footprint Smilie

Code:
#!/bin/sh
first=`egrep -n "^$1" $2 | cut -d ':' -f 1 | head -1`
last=`egrep -n "^$1" $2 | cut -d ':' -f 1 | tail -1`
head -$last $2 | tail +$first | egrep -v "^$1"

usage: scriptname.sh localhost0 filename.log
If you want to keep displaying the 'localhost0' lines too, just leave off the last egrep -v
# 3  
Old 01-07-2008
Code:
awk '/^localhost0/,/^localhost0: exit/' file

# 4  
Old 01-07-2008
Quote:
Originally Posted by shamrock
Code:
awk '/^localhost0/,/^localhost0: exit/' file

My awk's not the best but wouldn't that only print the lines between the first and second ^localhost0? Rather than the first and last?
# 5  
Old 01-07-2008
With localhost0 lines:
Code:
awk '/^localhost0/ { min = min ? min : NR ; max = NR }  min { line[NR] = $0 } END { for ( i = min ; i <= max ; i++ ) { print line[i]}}' file

without:
Code:
awk '/^localhost0/ { min = min ? min : NR ; max = NR }  min { line[NR] = $0 } END { for ( i = min + 1 ; i < max ; i++ ) { print line[i]}}' file

# 6  
Old 01-08-2008
Quote:
Originally Posted by Smiling Dragon
My awk's not the best but wouldn't that only print the lines between the first and second ^localhost0? Rather than the first and last?
It will print all lines between and including the lines that start with ^localhost0. Isn't that the desired output.? Smilie
# 7  
Old 01-08-2008
Quote:
Originally Posted by shamrock
It will print all lines between and including the lines that start with ^localhost0. Isn't that the desired output.? Smilie
Quote:
Originally Posted by jjinno
What I want is to get the first occurrence of "^localhost0" (at line start) and then everything else up to the last occurrence of "^localhost0"
Gotta be precise here Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Print 4th line back from regexp

I'm looking for a way to print the 4th line back from a regular expression. Kind of like the below but it has to be the 4th line before the regexp. Print the line immediately before regexp, but not the line containing the regexp. sed -n '/regexp/{g;1!p;};h' here is an example of logs(i... (11 Replies)
Discussion started by: senormarquez
11 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

sed print first line before regexp and all lines after

Hi All I'm trying to extract the line just above a regexp and all lines after this. I'm currently doing this in two steps sed -n -e "/^+---/{g;p;}" -e h oldfile.txt > modified.txt sed -e "1,/^+---/d" -e "/^$/d" oldfile.txt >>modified.txt Sample sometext will be here sometext will be... (3 Replies)
Discussion started by: Celvin VK
3 Replies

5. Shell Programming and Scripting

Print lines between a regExp & a blank line

Hi, I have a file, say files_list, as below (o/p of ls -R cmd) $ cat files_list /remote/dir/path/to/file: sub-dir1 sub-dir2 sub-dir3 ... /remote/dir/path/to/file/sub-dir1: remote_file1.csv.tgz <blank line 1> /remote/dir/path/to/file/sub-dir2: remote_file2.csv.tgz <blank... (3 Replies)
Discussion started by: dips_ag
3 Replies

6. Shell Programming and Scripting

use regexp to insert newline within a line

I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. However, I want to insert a newline immediately following /WORD/ and not after the -line- containing the pattern matched. I can match a pattern, but it is matched via a wild card... (2 Replies)
Discussion started by: kpeirce
2 Replies

7. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

8. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies

9. Shell Programming and Scripting

regexp to get first line of string

Hi everybody for file in * #Bash performs filename expansion #+ on expressions that globbing recognizes. do output="`grep -n "$1" "$file"`" echo "$file: `expr "$output" : '\(^.*$\)'`" done In the above bash script segment, I try to print just the first line of string named... (3 Replies)
Discussion started by: jonas.gabriel
3 Replies

10. Shell Programming and Scripting

Finding occurrance of a value in a variable

Hi, I was given a question like this: Write a program which reads a set of arguments from the standard input. Note that there is no limit for the number of arguments. The first argument is the file name and the other arguments are searched in the given file. For each argument, it displays... (3 Replies)
Discussion started by: ambitious
3 Replies
Login or Register to Ask a Question