Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

String parsing help across multiple UNIX platforms

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 2 Weeks Ago
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 June 2017, 9:52 AM EDT
Location: Aachen, Germany
Posts: 10,953
Thanks: 277
Thanked 3,367 Times in 3,100 Posts
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}

Sponsored Links
    #16  
Old Unix and Linux 2 Weeks Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 24 June 2017, 4:27 AM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,626 Times in 3,092 Posts
Quote:
Originally Posted by RudiC View Post
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
RudiC (2 Weeks Ago)
Sponsored Links
    #17  
Old Unix and Linux 1 Week Ago
harleyvrodred harleyvrodred is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 10 June 2017, 3:29 PM EDT
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
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; 1 Week Ago at 03:29 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing OSX UNIX command results which print in multiple lines hungryd Shell Programming and Scripting 7 12-02-2015 12:31 PM
Specific string parsing in Linux/UNIX satishrao Shell Programming and Scripting 1 05-13-2014 12:18 PM
string parsing using UNIX ali123 Shell Programming and Scripting 4 11-09-2011 10:18 AM
Platforms using Unix Megadrink UNIX and Linux Applications 2 11-11-2009 08:53 PM
Parsing of file for Report Generation (String parsing and splitting) umar.shaikh Shell Programming and Scripting 8 03-02-2009 12:38 AM



All times are GMT -4. The time now is 09:52 AM.