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?
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
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
4. UNIX for Dummies Questions & Answers
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. UNIX for Beginners Questions & Answers
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
LEARN ABOUT DEBIAN
ppi::token::quotelike::readline
PPI::Token::QuoteLike::Readline(3pm) User Contributed Perl Documentation PPI::Token::QuoteLike::Readline(3pm)
NAME
PPI::Token::QuoteLike::Readline - The readline quote-like operator
INHERITANCE
PPI::Token::QuoteLike::Readline
isa PPI::Token::QuoteLike
isa PPI::Token
isa PPI::Element
DESCRIPTION
The "readline" quote-like operator is used to read either a single line from a file, or all the lines from a file, as follows.
# Read in a single line
$line = <FILE>;
# From a scalar handle
$line = <$filehandle>;
# Read all the lines
@lines = <FILE>;
METHODS
There are no methods available for "PPI::Token::QuoteLike::Readline" beyond those provided by the parent PPI::Token::QuoteLike, PPI::Token
and PPI::Element classes.
Got any ideas for methods? Submit a report to rt.cpan.org!
SUPPORT
See the support section in the main module.
AUTHOR
Adam Kennedy <adamk@cpan.org>
COPYRIGHT
Copyright 2001 - 2011 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
perl v5.10.1 2011-02-26 PPI::Token::QuoteLike::Readline(3pm)