Pulling specific characters from lines in Shell Scripts

Pulling specific characters from lines in Shell Scripts

Post by Mik » Thu, 24 Apr 2003 22:47:59



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.

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.

Thanks,
Mike

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.

 
 
 

Pulling specific characters from lines in Shell Scripts

Post by Tim Hayne » Thu, 24 Apr 2003 23:21:43



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

Awk will suffice for this. All you want to do is take the second column
(separated by whitespace) and print the results of dividing it by 1024, so
consider:

 | echo 'MIKE 123456 (KB)' | awk '{print $2/1024}'

for size.

The pertinent things to know about awk are that by default, it separates
based on (multiple) whitespace(s), and the scriptlet in that commandline is
a pair of predicate-action clauses, where in this case the predicate is
empty.

Assuming for the sake of example that you wanted only those lines in which
`mike' appeared in the left side, you could refine the command with a
filtering predicate, thus:

 | echo 'mike123 123456 (KB)' | awk '/^mike/ {print $2/1024}'

which matches only *if* the line begins with `mike' at the start, *then*
print the second column divided by 1024.

HTH,

~Tim
--

A golden highway to the sea of dreams       |http://spodzone.org.uk/

 
 
 

Pulling specific characters from lines in Shell Scripts

Post by Stephane CHAZELA » Thu, 24 Apr 2003 23:55:57



> 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

[...]

echo "MIKE 123456 (KB)" | awk '
  $3 == "(KB)" && $2 >= 1024 {$2 /= 1024; $3 = "(MB)"}1'

--
Stphane

 
 
 

Pulling specific characters from lines in Shell Scripts

Post by Chris F.A. Johnso » Fri, 25 Apr 2003 02:33:30



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

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

Quote:}

   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

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

Pulling specific characters from lines in Shell Scripts

Post by Chris Co » Fri, 25 Apr 2003 03:56:32



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

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

> Thanks,
> Mike

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

If the value is in a variable...

val="   MIKE 123456 (KB)"
valk=`expr "${val}" : ".* \([0-9][0-9]*\) (KB)"`

However since you want to also do computation (for M instead of K),
you might be better off with an awk solution.  With awk
you can get the value and manipulate it.

If awk is too much overhead, you can also use bc to do
the math:

valm=`echo "scale=2\n${valk} / 1024" | bc`

(assumes a SysV like echo... on bash might have to use echo -e)

 
 
 

1. Question about shell script, can you enter a script at a specific line?

I have all this .mp3 files, in a file like this:

while true
do

<snip>
mpg123 /big/sound/you_are_my_inspiration/Thank_You.mp3
mpg123 /big/sound/you_are_my_inspiration/Heartsong.mp3
mpg123 /big/sound/you_are_my_inspiration/Sunrise.mp3  

mpg123 /big/sound/heartfelt/Focus.mp3
mpg123 /big/sound/heartfelt/Never_Apart.mp3
mpg
<snip>

done

big is my windows disk, not much there, so I keep it for music BTW.

The file is very long, and will play music for many hours.
I would like to be able to stop it, and the restart at the latest point.
(I know about ctrl Z, but say a reboot).

I know the title, but how can I enter this very long script at a specific
line?

Or could I call the script with an argument, how do I code in the simplest
way so it starts at a specific line?

I am not very good as 'bash', could do it in C perhaps.
I did read man bash...

But maybe someone knows a simple way to solve this :-)?
Regards
Jan

2. Building dynamic commands with eval

3. how to split input line to one character per line with just shell

4. EISA Devices

5. telnet line-by-line to character-by-character

6. FIPS and my partitions

7. sed insert character in specific position relative to end of line

8. Linux Virtual resizable partitions ?

9. Check for a character in a specific position on a line

10. # How to read a textfile line by line using csh shell script ?

11. unix shell script to access a file line by line

12. How to read text line-by-line in shell script?

13. filter lines with a specific expression at a specific position