Unix Shell - Help with String Extraction

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > Help with String Extraction





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Help with String Extraction
Vishal Sharma

2007-09-26, 7:31 am

Hello All,

My requirement is:

This is my file status.txt

server0 up 6 days 4:42 load average: 0.01 0.02 0.03
server1 up 8 days 22:54 load average: 0.01 0.06 0.05
server2 up 4 days 2:03 load average: 0.02 0.01 0.01
server3 up 7 days 22:46 load average: 0.01 0.03 0.04

i want to extract parse this file such that the output looks like
this:

server0 load average: 0.01 0.02 0.03
server1 load average: 0.01 0.06 0.05
server2 load average: 0.02 0.01 0.01
server3 load average: 0.01 0.03 0.04

How do i use sed/awk combination to achieve the above output.

Thanks in advance.

Regards,
Vishal Sharma

mr.bmonroe@gmail.com

2007-09-26, 1:26 pm

On Sep 26, 3:22 am, Vishal Sharma <visha...@gmail.com> wrote:
> This is my file status.txt
>
> server0 up 6 days 4:42 load average: 0.01 0.02 0.03
> server1 up 8 days 22:54 load average: 0.01 0.06 0.05
> server2 up 4 days 2:03 load average: 0.02 0.01 0.01
> server3 up 7 days 22:46 load average: 0.01 0.03 0.04
>
> i want to extract parse this file such that the output looks like
> this:
>
> server0 load average: 0.01 0.02 0.03
> server1 load average: 0.01 0.06 0.05
> server2 load average: 0.02 0.01 0.01
> server3 load average: 0.01 0.03 0.04
>
> How do i use sed/awk combination to achieve the above output.


Why use sed or awk when you can do it in the shell?

$ while read -r <&3 a b c d e f
> do
> printf "%s\t\t%s\n" "${a}" "${f}"
> done 3<status.txt


Thanks
--Brett

Ed Morton

2007-09-26, 1:26 pm

mr.bmonroe@gmail.com wrote:
> On Sep 26, 3:22 am, Vishal Sharma <visha...@gmail.com> wrote:
>
>
>
> Why use sed or awk when you can do it in the shell?


Because either sed and awk are the right tools for the job.

> $ while read -r <&3 a b c d e f
>
>


Either of these is simpler:

sed 's/ .*load/ load/' status.txt
awk 'sub(/ .*load/," load")' status.txt

Regards,

Ed.
Chris F.A. Johnson

2007-09-26, 7:20 pm

On 2007-09-26, mr.bmonroe@gmail.com wrote:
> On Sep 26, 3:22 am, Vishal Sharma <visha...@gmail.com> wrote:
>
> Why use sed or awk when you can do it in the shell?


If the file is more than a fairly small number of lines (25? 50?
100?), it will execute much more slowly than sed or awk.
[vbcol=seagreen]
> $ while read -r <&3 a b c d e f


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane CHAZELAS

2007-09-26, 7:20 pm

2007-09-26, 17:47(-04), Chris F.A. Johnson:
[...]
>
> If the file is more than a fairly small number of lines (25? 50?
> 100?), it will execute much more slowly than sed or awk.

[...]

Agreed, but overall, a shell being over and before all a command
line interpreter doing something "in the shell" should mean, do
it with some commands called by the shell.

--
Stéphane
Chris F.A. Johnson

2007-09-26, 7:20 pm

On 2007-09-26, Stephane CHAZELAS wrote:
> 2007-09-26, 17:47(-04), Chris F.A. Johnson:
> [...]
> [...]
>
> Agreed, but overall, a shell being over and before all a command
> line interpreter


The shell is a command line interpreter that includes a full
programming language. There's no good reason not to use it as such.

> doing something "in the shell" should mean, do it with some commands
> called by the shell.


That is not true even with a Bourne shell, though then you _would_
have to use more external commands. There is absolutely no
justification for that attitude with a POSIX shell.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com