Solved

unix ksh: script needs to read values from external file into variables

Posted on 2011-03-17
8
751 Views
Last Modified: 2012-05-11
Dear Experts,

HPUX / ksh
In a script I need to create variables with values extracted from the DESCRIPTION with is attached to a CPUNAME (see 'input file' below for example).

The command would need to search for a certain CPUNAME in the file then:
1) from the line under it extract first value of the description, for instance [ R3 ]
2) extract second value of the description, for instance [ HR ]

The values are seperated by a dash [-].

#Example (sample input file is below)
CPUNAME to search for would be HT0016P

WKS=HT0016P
CATEGORY=[as described under 1) above ]
SUBCATEGORY=[as described under 2) above ]

# input_file
CPUNAME HRZ-000
  DESCRIPTION "R3 - HR - host is SAPHRD"
CPUNAME HRZ-750
  DESCRIPTION "R3 - HR - host is SAPHRD"
CPUNAME HT0016P
  DESCRIPTION "HPUX - TWS MASTER  - "
CPUNAME MAEAPPD01
  DESCRIPTION "SUN - ProIntralink - "

So in this case...
CPUNAME HT0016P
  DESCRIPTION "HPUX - TWS MASTER  - "

The variables would be translated into this:
category=HPUX
subcategory="TWS MASTER"


Can this be done?
Please let me know if this is not clear enough...
Thanks a lot.
0
Comment
Question by:Watnog
  • 5
  • 3
8 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35156158
awk '/HT0016P/ {getline; FS="\"" ; print $2}' inputfile | awk -F"-" '{print $1, $2} | read category subcategory

echo "$category"
echo "$subcategory"

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35156309
Let's make it a bit shorter:

awk '/HT0016P/ {getline; FS="[\"-]" ; print $2, $3}'  inputfile | read category subcategory

echo "$category"
echo "$subcategory"

wmp
0
 

Author Comment

by:Watnog
ID: 35156467
Thank you wmp.
The problem that remains, and which wasn't covered in my question, is that there can be more than one occurence of the workstation (unlike HT0016P for which this works fine - after adding a closing ['] after {print $1,$2}.

In the sample inputfile if you would do the command for SAPHRD, it doesn't work (you get two empty lines).
We would really have to look for the name that comes with the CPUNAME

CPUNAME HRZ-000
  DESCRIPTION "R3 - HR - host is SAPHRD"
CPUNAME HRZ-750
  DESCRIPTION "R3 - HR - host is SAPHRD"
CPUNAME HT0016P
  DESCRIPTION "HPUX - TWS MASTER  - "
CPUNAME MAEAPPD01
  DESCRIPTION "SUN - ProIntralink - "
CPUNAME SAPHRD
  DESCRIPTIION "HPUX - ERP - "

Glad you are helping me wmp.

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35156502
Sorry for the missing ' !

awk '/CPUNAME SAPHRD/ {getline; FS="[\"-]" ; print $2, $3}'  inputfile | read category subcategory

echo "$category"
echo "$subcategory"

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35156640
I think you would like to have the workstation name as a variable?
OK, we must make some changes to achieve this:

W="SAPHRD"
awk -F"[\"-]" "/CPUNAME $W/ {getline ; print \$2, \$3}" inputfile | read category subcategory

echo "$category"
echo "$subcategory"

Note that the field separator must now be defined using -F, the ' characters must be replaced by ", and the $ characters in the awk variables must be escaped.
0
 

Author Comment

by:Watnog
ID: 35156679
No sigar.
This is my line:
awk '/CPUNAME $wks/ {getline; FS="[\"-]" ; print $2, $3}'  cpu_description.txt | read category subcategory
echo "$category"
echo "$subcategory"

But nothing is returned.
Do I have to escape the $wks in some way?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35156721
See my last post (No, I can't read your mind, don't worry!)

Shell variables like your $wks are not expanded between single quotes, and using double quotes with awk is a bit tricky, see my solution.

0
 

Author Closing Comment

by:Watnog
ID: 35156801
Very very good again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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