• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

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

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
Watnog
Asked:
Watnog
  • 5
  • 3
1 Solution
 
woolmilkporcCommented:
awk '/HT0016P/ {getline; FS="\"" ; print $2}' inputfile | awk -F"-" '{print $1, $2} | read category subcategory

echo "$category"
echo "$subcategory"

wmp
0
 
woolmilkporcCommented:
Let's make it a bit shorter:

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

echo "$category"
echo "$subcategory"

wmp
0
 
WatnogAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
woolmilkporcCommented:
Sorry for the missing ' !

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

echo "$category"
echo "$subcategory"

0
 
woolmilkporcCommented:
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
 
WatnogAuthor Commented:
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
 
woolmilkporcCommented:
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
 
WatnogAuthor Commented:
Very very good again.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now