- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh: parse a file with different field separator
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2016 06:29 AM - edited тАО07-20-2016 06:30 AM
тАО07-20-2016 06:29 AM - edited тАО07-20-2016 06:30 AM
ksh: parse a file with different field separator
hello,
i have files ( example of data protector file "cell_info" ) with different fields and i think different field separators.
example :
-host "server_dp_06_20" -os "hp ia64 hp-ux-11.31" -core A.06.20 -integ A.06.20 -cs A.06.20 -da A.06.20 -ma A.06.20 -cc A.06.20 -javagui A.06.20 -oracle8 A.06.20 -docs A.06.20 -host "server_dp_09_20" -os "gpl x86_64 linux-2.6.16.60-0.103.1-smp" -core A.09.00 -integ A.09.00 -da A.09.00 -ma A.09.00 -cc A.09.00 -oracle8 A.09.00 -autodr A.09.00 -ts_core A.09.00 -corepatch A.09.06 -integpatch A.09.06 -dapatch A.09.06 -mapatch A.09.06 -ccpatch A.09.06 -oracle8patch A.09.06 -autodrpatch A.09.06 -ts_corepatch A.09.06
i want to get the value ( in double quotes ) after -host and -os. so in first line after -host : "server_dp_06_20" and after -os "hp ia64 hp-ux-11.31" . i tried while IFS="-" read value1 value1 etc and with awk i have problem the different fields. any idea ?
regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2016 06:59 AM
тАО07-20-2016 06:59 AM
Re: ksh: parse a file with different field separator
> I want to get the value ( in double quotes ) after -host [...]
mba$ line='-host "server_dp_06_20" -os "hp ia64 hp-ux-11.31" -core A.06.20'
mba$ echo "$line" | sed -e 's/-host "\([^"]*\)".*/\1/'
server_dp_06_20
Or, if you want to include the quotation marks in the result:
mba$ echo "$line" | sed -e 's/-host \("[^"]*"\).*/\1/'
"server_dp_06_20"
If you can't do it with "sed", then it's not worth doing, I always
say. This example was done on a Mac, but at my level of expertise,
"sed" is "sed".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2016 08:52 AM - edited тАО07-20-2016 08:54 AM
тАО07-20-2016 08:52 AM - edited тАО07-20-2016 08:54 AM
Re: ksh: parse a file with different field separator
My choice is to use awk to grab the fields you need using the -F \" option as the separator.
Here is the result with awk:
TXT='-host "server_dp_06_20" -os "hp ia64 hp-ux-11.31" -core A.06.20 -integ A.06...' -host "server_dp_09_20" -os "gpl x86_64 linux-2.6.16.60-0.103.1-smp" -core A.09....' echo "$TXT" | awk -F \" '{print $2,$4}' server_dp_06_20 hp ia64 hp-ux-11.31
server_dp_09_20 gpl x86_64 linux-2.6.16.60-0.103.1-smp
Since the data fields may contain spaces, you can run the awk command for each variable:
TXT='-host "server_dp_06_20" -os "hp ia64 hp-ux-11.31" -core A.06.20 -integ A.06...' -cs...'
-host "server_dp_09_20" -os "gpl x86_64 linux-2.6.16.60-0.103.1-smp" -core A.09...'
SERVERNAME="$(echo "$TXT" | awk -F \" '{print $2}')"
OS="$(echo "$TXT" | awk -F \" '{print $4}')"
echo "Server: $SERVERNAME, OS: $OS"
Server: server_dp_06_20, OS: hp ia64 hp-ux-11.3
If this script is being used to read a long file, just use read to assign the TXT variable:
cat $SOMEFILE | while read TXT do SERVERNAME="$(echo "$TXT" | awk -F \" '{print $2}')" OS="$(echo "$TXT" | awk -F \" '{print $4}')" echo "Server: $SERVERNAME, OS: $OS" done
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2016 09:10 AM
тАО07-20-2016 09:10 AM
Re: ksh: parse a file with different field separator
> My choice is to use awk to grab the fields you need [...]
> [...] $2,$4 [...]
Ok, if you _know_ in which order the fields apppear.
> I want to get the value ( in double quotes ) after -host [...]
Knowing nothing about the source of these data, if someone tells me
"after -host", then I tend to look for "-host", and take what comes
after it. Someone who knows more than I may assume things which I
won't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2016 01:13 AM - edited тАО07-22-2016 01:15 AM
тАО07-22-2016 01:13 AM - edited тАО07-22-2016 01:15 AM
Re: ksh: parse a file with different field separator
The man page of the hp-ux version awk that I tried this on does not document support gensub or any other method of using a captured group as replacement text. So something like this may work:
awk 'match($0, "-host [^ ]* -os [^ ]*") { $0=substr($0, RSTART, RLENGTH); sub("-host ",""); sub ("-os ", ""); print; }'
--
ranga
[i work for hpe]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2016 03:23 AM - edited тАО07-24-2016 03:47 AM
тАО07-24-2016 03:23 AM - edited тАО07-24-2016 03:47 AM
Re: ksh: parse a file with different field separator
You you can write a program in awk to look at each field.
awk '
{
host = ""
os = ""
for (i = 1; i < NF; ++i) {
if ($i == "-os") {
os = $(i+1)
++i
# find next quote
for (;i < NF; ) {
j = index(substr(os, 2), "\"")
if (j > 0) break
os = os " " $(i+1)
++i
}
} else if ($i == "-host") {
host = $(i+1)
++i
}
}
print "host=" host "; os=" os
}' input-file