Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


use sed, awk, or ?  get a substr from file using shell, no perl

Posted on 1999-07-31
Medium Priority
Last Modified: 2013-12-16
what command: sed, awk or ? should I use to execute in a bourne shell to read the following from a text file:

calls: 21   minutes: 34   available: 0 (0)

and put into 3 vars, example:
$calls = 21
$min = 34
$avail = 0 (the first zero, ignore second)
Question by:ToddRod
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 84

Expert Comment

ID: 1813433
calls=`awk '{print $2}' file`
min=`awk '{print $4}' file`
avail=`awk '{print $6}' file`

Expert Comment

ID: 1813434
read crap calls crap min crap avail crap < file
unset crap
LVL 51

Accepted Solution

ahoffmann earned 400 total points
ID: 1813435
eval `cat file|tr -d ':'|awk '{printf "%s=%s;%s=%s;%s=%s;",$1,$2,$3,$4,$5,$6}'`
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1813436
I like ahoffmann's response, however, it doesn't put the values read into vars it seems; it just prints the output as:

LVL 84

Expert Comment

ID: 1813437
Did you use the same kind of quotes that ahoffmann specified?
LVL 51

Expert Comment

ID: 1813438
as ozo said, the outermost quotes are backtick (or gravis, or whatever they are called: top-left to bottom-right).

Author Comment

ID: 1813439
ahoffmann, my bad!

I was "lazy" and did the copy/paste thing; it conviently removed the ` from the beginning and end of eval statment

as we say in my office, "You da man!" (darn, it's not me this time!)


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question