Solved

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

Posted on 2011-03-17
8
750 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
Gigs: Get Your Project Delivered by an Expert

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

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

786 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