Re: Pulling specific characters from lines in Shell Scripts

From: Chris F.A. Johnson (c.f.a.johnson@rogers.com)
Date: 04/23/03


From: "Chris F.A. Johnson" <c.f.a.johnson@rogers.com>
Date: 23 Apr 2003 17:33:30 GMT

On Wed, 23 Apr 2003 at 13:47 GMT, Mike wrote:
> My basic problem is that I have a script that outputs a line with text
> and a number, i.e. MIKE 123456 (KB)
>
> I would like to format that number in Megabytes to make it easier for
> users to understand; however, I can't quite figure out how to just
> "capture" the number part of the line in order to perform the
> calculation with it and re-output it.

   Why not change the script to output what you want?

> I'm thinking that awk and/or sed would be my two best ways but I can't
> figure out the exact syntax. I would appreciate any help.

   There is no need to use an external command to split up a line. Assuming
   the format of the output remains the same, you can use read to do it:

script | { read name number units
  ## modify $number here
}

   Or use the output of the script as positional parameters:

set -- `script`
name=$1
number=$2
units=$3

   Or, if the format may vary, use a for loop:

for word in `script`
do
     case $word in
        *[!0-9]*) ;; ## not a number
        *) ## process number here
         ;;
     esac
done

> P.S. Just thought I'd mention I'm a newbie at shell scripting so if
> possible just mention in brief the why behind the what. If I
> understand the why I won't have to come back asking the same dumb
> question.

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: LDAP be killing me. I need a good step by step
    ... I wrote a perl script to parse this and put it into a valid ldif format: ... Thanks for your script, which I shall study. ... For example, I use kmail, which claims to understand LDAP. ...
    (Fedora)
  • Re: LDAP be killing me. I need a good step by step
    ... I wrote a perl script to parse this and put it into a valid ldif format: ... Thanks for your script, which I shall study. ... For example, I use kmail, which claims to understand LDAP. ...
    (Fedora)
  • Re: Lawrence Miles Doctor Who script
    ... (a former New Adventures / Eight Doctor Adventures author, ... telly-Who-only readers) has stuck a Doctor Who script online. ... But in saveable format. ... "dead" story available in this way but in an editable format. ...
    (rec.arts.drwho)
  • Re: Microsoft Excel help/advice?
    ... do a partial script on spec to show them she can do it, ... So she needs to write the scipt in MS Excel, ... I have personally neve seen a spreadsheet and was never clear on what ... if it will be efficient for us to work in this format, ...
    (rec.music.artists.springsteen)
  • Re: media player tools
    ... The script can either rename the ... > library to a specified WMA format using WME9. ... > It is useful if you ever get pops/ticks when ripping CDs with WMP9 and ...
    (microsoft.public.windowsmedia.player)