Help with String Extraction

Help with String Extraction

am 26.09.2007 12:22:40 von vishal78

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

Re: Help with String Extraction

am 26.09.2007 16:44:12 von mr.bmonroe

On Sep 26, 3:22 am, Vishal Sharma 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
Thanks
--Brett

Re: Help with String Extraction

am 26.09.2007 17:07:08 von Ed Morton

mr.bmonroe@gmail.com wrote:
> On Sep 26, 3:22 am, Vishal Sharma 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?

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

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

Either of these is simpler:

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

Regards,

Ed.

Re: Help with String Extraction

am 26.09.2007 23:47:38 von cfajohnson

On 2007-09-26, mr.bmonroe@gmail.com wrote:
> On Sep 26, 3:22 am, Vishal Sharma 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?

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.

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

--
Chris F.A. Johnson, author
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

Re: Help with String Extraction

am 27.09.2007 00:19:45 von Stephane CHAZELAS

2007-09-26, 17:47(-04), Chris F.A. Johnson:
[...]
>>> 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?
>
> 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

Re: Help with String Extraction

am 27.09.2007 01:27:24 von cfajohnson

On 2007-09-26, Stephane CHAZELAS wrote:
> 2007-09-26, 17:47(-04), Chris F.A. Johnson:
> [...]
>>>> 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?
>>
>> 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

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
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