Help with String Extraction
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Help with String Extraction




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Help with String Extraction  
Vishal Sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 12:31 PM

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






[ Post a follow-up to this message ]



    Re: Help with String Extraction  
mr.bmonroe@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 06: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






[ Post a follow-up to this message ]



    Re: Help with String Extraction  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-26-07 06: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.





[ Post a follow-up to this message ]



    Re: Help with String Extraction  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-27-07 12:20 AM

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





[ Post a follow-up to this message ]



    Re: Help with String Extraction  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-27-07 12:20 AM

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





[ Post a follow-up to this message ]



    Re: Help with String Extraction  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-27-07 12:20 AM

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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register