Re: Pulling specific characters from lines in Shell Scripts
From: Chris F.A. Johnson (c.f.a.johnson@rogers.com)
Date: 04/23/03
- Next message: Ken D: "TCP Socket Using connect in Non Blocking - Results in errors using recv()"
- Previous message: Bill Marcum: "Re: SIGTTOU problem"
- In reply to: Mike: "Pulling specific characters from lines in Shell Scripts"
- Next in thread: Chris Cox: "Re: Pulling specific characters from lines in Shell Scripts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Ken D: "TCP Socket Using connect in Non Blocking - Results in errors using recv()"
- Previous message: Bill Marcum: "Re: SIGTTOU problem"
- In reply to: Mike: "Pulling specific characters from lines in Shell Scripts"
- Next in thread: Chris Cox: "Re: Pulling specific characters from lines in Shell Scripts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|