String parsing help across multiple UNIX platforms


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String parsing help across multiple UNIX platforms
# 15  
Old 06-10-2017
Wildly guessing on what you might want, how about
Code:
awk '
NR == 1         {for (n=split(PL, PA, "|"); n>0; n--) PAT[PA[n]]
                }
                {for (p in PAT) if (match ($0, p))      {TMP = substr ($0, RSTART, RLENGTH)
                                                         gsub (/^.*=|"|> *$/, "")
                                                         n = split ($0, UN)
                                                         UN[0] = 1 
                                                         getline
                                                         gsub (/<[^>]*>/, "")
                                                         OUT[p] = sprintf ("{\"%s\" %.1f %s}", TMP, UN[n-1] * $0, UN[n]) 
                                                        }
                }
END             {for (p in PAT) print OUT[p]
                }
' PL="Coolant Temp|Actual Air Velocity|Cabinet Temp" file
{"Actual Air Velocity" 869.0 fpm}
{"Coolant Temp" 30.7 C}
{"Cabinet Temp" 36.0 C}

# 16  
Old 06-10-2017
Quote:
Originally Posted by RudiC
Wildly guessing on what you might want, how about
Code:
awk '
NR == 1         {for (n=split(PL, PA, "|"); n>0; n--) PAT[PA[n]]
                }
                {for (p in PAT) if (match ($0, p))      {TMP = substr ($0, RSTART, RLENGTH)
                                                         gsub (/^.*=|"|> *$/, "")
                                                         n = split ($0, UN)
                                                         UN[0] = 1 
                                                         getline
                                                         gsub (/<[^>]*>/, "")
                                                         OUT[p] = sprintf ("{\"%s\" %.1f %s}", TMP, UN[n-1] * $0, UN[n]) 
                                                        }
                }
END             {for (p in PAT) print OUT[p]
                }
' PL="Coolant Temp|Actual Air Velocity|Cabinet Temp" file
{"Actual Air Velocity" 869.0 fpm}
{"Coolant Temp" 30.7 C}
{"Cabinet Temp" 36.0 C}

If there are a lot of strings in PL and one or more of those strings might not appear in all files that will be processed, you might want to change the END clause to:
Code:
END             {for (p in OUT) print OUT[p]
                }

to avoid printing empty lines. Or, change:
Code:
NR == 1         {for (n=split(PL, PA, "|"); n>0; n--) PAT[PA[n]]
                }

to something like:
Code:
NR == 1         {for (n=split(PL, PA, "|"); n>0; n--)   {PAT[PA[n]]
                                                         OUT[PA[n]] = sprintf("{\"%s\" Not Found}")
                                                        }
                }

to print an indication of which strings were not found in the file being processed. But, of course, I still think we need a clear specification of the desired behavior from harleyvrodred.
This User Gave Thanks to Don Cragun For This Post:
# 17  
Old 06-10-2017
I was looking to reduce the script as much as possible

I'm looking at your comments now and trying just that, thanks

Okay, I cannot use sub(), match(), and gsub() as they are not supported on all the platforms. That breaks the original requirement.

Last edited by harleyvrodred; 06-10-2017 at 04:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing OSX UNIX command results which print in multiple lines

from the CLI on a Mac, if you type networksetup -listallnetworkservices then you get results in a multi-line paragraph that look something like this: networksetup -listallnetworkservices An asterisk (*) denotes that a network service is disabled. Wi-Fi Display Ethernet Bluetooth DUN... (7 Replies)
Discussion started by: hungryd
7 Replies

2. Shell Programming and Scripting

Specific string parsing in Linux/UNIX

Hi, I have a string which can be completely unstructred. I am looking to parse out values within that String. Here is an example <Random Strings> String1=<some number a> String2=<some number b> String3=<some number c> Satish=<some number d> String4=<some number e> I only want to parse out... (1 Reply)
Discussion started by: satishrao
1 Replies

3. Shell Programming and Scripting

Multichecks across all unix platforms

Can somebody refer me following multicheck to perform across most of unix platform like AIX, HP-UX, solaris, Linux. CPU utilization above X% Check IO above X% Swap usage check above X% Memory utilization above X% ... (3 Replies)
Discussion started by: sendtoshailesh
3 Replies

4. Shell Programming and Scripting

string parsing using UNIX

I got multple sql files.such as >>vi abc.sql select A.SITENAME, NULL NULL A.CREATE_DTM NULL A.MODIFY_DTM NULL FROM ${STG_RET_ITEM} A INNER JOIN ${STG_INC_COMP} B ON (A.CUSTID=B.CUSTID) LEFT OUTER JOIN ( select C.SITEID,SITESTATUS,MIN_EFF_DT,CURR_ST_DT,MAX_IN_DT,MAX_ACT_DT from... (4 Replies)
Discussion started by: ali123
4 Replies

5. Programming

How do I find the MAC address in C on different UNIX platforms?

I need to find the MAC address of the ethernet cards on the host machine from the C language. I have found a way to do this on Linux using socket(), ioctl() and the ifreq structure. But this does not seem to work on AIX, HP/UX and probably the others I need (Solaris, SCO, Alpha etc). Is there a... (7 Replies)
Discussion started by: Pug
7 Replies

6. Shell Programming and Scripting

Logfile parsing with variable, multiple criterias among multiple lines

Hi all I've been working on a bash script parsing through debug/trace files and extracting all lines that relate to some search string. So far, it works pretty well. However, I am challenged by one requirement that is still open. What I want to do: 1) parse through a file and identify all... (3 Replies)
Discussion started by: reminder
3 Replies

7. UNIX and Linux Applications

Platforms using Unix

Hi ;) Which hardware platforms/machine types use the Operating System Unix? A list of all would be appreaciated Thx Megadrink :cool: (2 Replies)
Discussion started by: Megadrink
2 Replies

8. UNIX for Dummies Questions & Answers

Unix Platforms

Hi. Where can i learn, Which Unix platform specific for what? Properties? (1 Reply)
Discussion started by: Enrgy
1 Replies

9. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

10. UNIX for Advanced & Expert Users

Patch Management over mixed unix platforms

Does anyone know of any tools that manage the rollout of patches across multiple types of Unix platform ( eg Solaris, Aix etc ). I am looking for something that does a similiar job to SMS or WSUS in the Windows world (3 Replies)
Discussion started by: jimthompson
3 Replies
Login or Register to Ask a Question