Solved

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

Posted on 2011-03-17
8
749 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now