LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2006, 12:07 AM   #1
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Rep: Reputation: 30
sed script


how can i extract next 2 chars after some word from line from some given log file.
eg. say i have
this is log file for deamon 1: which runs deamon program
so what i want to extract 1 that mean i can say chars befors :
 
Old 06-17-2006, 03:51 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You could use
Code:
sed -ne "s/^[^:]*\(.\{2\}\):.*$/\1/p"
It will look for the first colon in a line and discard everything but the preceding two characters.
 
Old 06-17-2006, 04:34 AM   #3
cranium2004
Member
 
Registered: Feb 2004
Distribution: FC4,RHEL4
Posts: 223

Original Poster
Rep: Reputation: 30
thanks it worked. But can u explain it to me. also what i need to do same from doing it from last to fiest chars?? I mean your code searches for : from first char but same how can i do from last char
 
Old 06-17-2006, 05:18 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
"^" and "$" correspond to beginning/end of line. Then "[^:]*:" matches an arbitrary number of non-colon characters followed be a colon. We want to remember the two characters that precede the colon, so ":" is preceded by "\(.\{2\}\)". Then follows an arbitrary number of arbitrary characters, as indicated by ".*". Finally, "\1" replaces the whole line with the first content we remembered, i.e. the two characters preceding the colon. The command "p" tells sed to print the result.
If you want to look for the last colon instead of the first, it seems like "[^:]*" and ".*" should switch their roles somehow. Try it yourself.
 
Old 06-21-2006, 09:33 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Let me make 2 suggestions for improving the legibility of sed scripts. (So often the juxtapostion of all the '/' & '\' characters make them look like perl, or /dev/random, or Chuck Norris sparring w/ David Carradine.)

1. Utilize ',' or anything you like for the separator. sed uses the next character after 's' as the separator & it can be anything you like -- i.e. find legible. Thus, "sed 's, ... , ... ,' " works fine.

2. When using a lot of characters -- {}() -- that need escaping in sed to become metacharacters, use the "r" option to make them meta from the beginning.

3. It is customary to single quote sed's commands.

Thus, that nice little sed command becomes:
Code:
sed -rne 's,^[^:]*(.{2}):.*$,\1,p'
& can be tested like this:
Code:
echo 'this is log file for deamon 1: which runs deamon program'  |\
sed -rne 's,^[^:]*(.{2}):.*$,\1,p'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick SED Script windisch Programming 7 06-07-2006 02:43 AM
sed script glam1 Programming 2 08-31-2004 11:28 AM
sed script help/ideas rjcrews Programming 2 07-30-2004 02:59 PM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 09:40 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM


All times are GMT -5. The time now is 03:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration