Re: [SLUG] Extracting string from a file - shell script
> "grove" == grove writes: grove> On Fri, 3 Jul 2009, Peter Chubb wrote: >> Why use cat? >> >> Why not > >> The extra process plus pipe just wastes resources. grove> You've been reading too much Randal Schwartz.. ;) grove> http://sial.org/howto/shell/useless-cat/ No I've never read that. But I come from a background running 60 users on a pdp11-70 with 256k of core memory --- anything that used unnecessary processes or memory was evil. -- Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au http://www.ertos.nicta.com.au ERTOS within National ICT Australia -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
On Fri, 3 Jul 2009, Peter Chubb wrote: Why use cat? Why not You've been reading too much Randal Schwartz.. ;) http://sial.org/howto/shell/useless-cat/ rachel -- Rachel Polanskis Kingswood, Greater Western Sydney, Australia gr...@zeta.org.auhttp://www.zeta.org.au/~grove/grove.html "The perversity of the Universe tends towards a maximum." - Finagle's Law -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
Thanks all. Multiple options. Will give them a go. Kind Regards Kyle -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
On Fri, Jul 3, 2009 at 9:08 AM, Kyle wrote: > Hi Folks, > > I am trying to extract a substring from a string found in a file. > > The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - RC > = 0 > > and the substring I want to extract is TF0220. This is a program name and > the length of this name varies. In other words I want to extract whatever is > between the words "of" and "at" in a script. If the name is always at the same position in the string(ie, the line always starts with "*** End of " - "awk '{ print $4 ;}'" will do most of what you want. saga:~/src polleyj$ echo '*** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - RC = 0' | awk '{ print $4 ;}' TF0220 You probably want to do something extra to select out only lines matching this pattern - you can do this by stipulating a match to awk - ie, make it "awk '/End of/ { print $4; }'" > > How would I likely go about that please? > > -- > > Kind Regards > > Kyle > > -- > SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ > Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html > -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
Peter Chubb wrote: >> "Phil" == Phil Scarratt writes: > Phil> Something like this would work: > > Phil> cat | perl -nle > Phil> 'if(/^\*\*\*\s+End\s+of\s+([\S]+)\s+at\s+.*/){print $1;}' > > > Why use cat? > > Why not > The extra process plus pipe just wastes resources. > Good point! But then again sed is probably a better option anyway Fil -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
> "Phil" == Phil Scarratt writes: Phil> Kyle wrote: >> Hi Folks, >> >> I am trying to extract a substring from a string found in a file. >> >> The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - >> RC = 0 >> >> and the substring I want to extract is TF0220. This is a program >> name and the length of this name varies. In other words I want to >> extract whatever is between the words "of" and "at" in a script. >> >> How would I likely go about that please? >> Phil> Something like this would work: Phil> cat | perl -nle Phil> 'if(/^\*\*\*\s+End\s+of\s+([\S]+)\s+at\s+.*/){print $1;}' Why use cat? Why not http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au http://www.ertos.nicta.com.au ERTOS within National ICT Australia -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
Kyle wrote: > Hi Folks, > > I am trying to extract a substring from a string found in a file. > > The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - > RC = 0 > > and the substring I want to extract is TF0220. This is a program name > and the length of this name varies. In other words I want to extract > whatever is between the words "of" and "at" in a script. > > How would I likely go about that please? > Something like this would work: cat | perl -nle 'if(/^\*\*\*\s+End\s+of\s+([\S]+)\s+at\s+.*/){print $1;}' Fil -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
> "Kyle" == Kyle writes: Kyle> Hi Folks, Kyle> I am trying to extract a substring from a string found in a Kyle> file. Kyle> The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 Kyle> - RC = 0 Simplest way is sed. sed -n -e 's/^.*End of \([^ ]*\) at .*$/\1/p' filename The regular expression matches an entire line, and throws away everything but the bit you want. I'm assuming that the bit you want doesn't contain spaces, if it does, you need a different regular expression. -- Dr Peter Chubb http://www.gelato.unsw.edu.au peterc AT gelato.unsw.edu.au http://www.ertos.nicta.com.au ERTOS within National ICT Australia -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
Re: [SLUG] Extracting string from a file - shell script
in perl, depending on how strictly you want to enforce the format of the TF0220 (in this case, just any string between 'End of' and 'at'. #!/usr/bin/perl use warnings; use strict; my $string = q|*** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - RC = 0|; my ($var) = $string =~ /End\s+of\s+([\w\d]+)\s+at/; print $var if $var; Dean Kyle wrote: Hi Folks, I am trying to extract a substring from a string found in a file. The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - RC = 0 and the substring I want to extract is TF0220. This is a program name and the length of this name varies. In other words I want to extract whatever is between the words "of" and "at" in a script. How would I likely go about that please? -- http://fragfest.com.au -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
[SLUG] Extracting string from a file - shell script
Hi Folks, I am trying to extract a substring from a string found in a file. The string is: *** End of TF0220 at Thu Jul 2 10:06:51 EST 2009 - RC = 0 and the substring I want to extract is TF0220. This is a program name and the length of this name varies. In other words I want to extract whatever is between the words "of" and "at" in a script. How would I likely go about that please? -- Kind Regards Kyle -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html