Solved

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

Posted on 2011-03-17
8
748 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.

744 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

13 Experts available now in Live!

Get 1:1 Help Now