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
    #8  
Old Unix and Linux 2 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 3:14 AM EDT
Location: Aachen, Germany
Posts: 10,959
Thanks: 280
Thanked 3,368 Times in 3,101 Posts
Small correction to Don Cragun's fine proposal: in the sprintf command, replace "pat" by "pattern" to get the desired result.


Use this for the last occurrence of the pattern in file:

Code:
awk '
match ($0, /"Coolant Temp"/)    {TMP = substr ($0, RSTART, RLENGTH)
                                 gsub (/^.*=|"|> *$/, "")
                                 split ($0, UN)
                                 getline
                                 gsub (/<[^>]*>/, "")
                                 OUT = sprintf ("{%s %.1f %c}", TMP, UN[1] * $0, UN[2]) 
                                }
END                             {print OUT
                                }
' file


Last edited by RudiC; 2 Weeks Ago at 04:23 AM..
The Following 2 Users Say Thank You to RudiC For This Useful Post:
Don Cragun (2 Weeks Ago), harleyvrodred (2 Weeks Ago)
Sponsored Links
    #9  
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: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Hi RudiC,
I should know better than to try to make code easier to read after it has been tested. Linux

My post #7 has now been updated as you suggested.

Thanks,
Don
The Following User Says Thank You to Don Cragun For This Useful Post:
harleyvrodred (2 Weeks Ago)
Sponsored Links
    #10  
Old Unix and Linux 2 Weeks 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
Nice examples. I took this and made a command I could put into the clipboard and paste into a telnet window. Because I want to select-cut the result into the clipboard I'm wrapping the output onto as few lines as possible.


Code:
sh << 'PASTE'
xml_token () {
 variable=$(awk -F'"' -v pattern="$1" '
   $2 == pattern {
    split($4, t, / /)
    getline
    split($0, m, /[<>]/)
    o = sprintf("{%s %.1f %s}", FS pattern FS, t[1] * m[3], t[2])
   }
  END {	print o
   }' $2)
 if [ $(( ${#buff} + ${#variable} )) -ge $twidth ]; then
  echo $buff; buff=$variable
   else
  buff+=$variable
   fi
}
twidth=`tput cols`
xml_token 'Coolant Temp' ~me/cooling.xml
xml_token 'Cabinet Temp' ~me/cooling.xml
echo $buff
'PASTE'

The Output using the example file is:

Code:
{"Coolant Temp" 30.7 C}{"Cabinet Temp" 36.0 C}

I was thinking about the possibility of making the search pattern string contain a list, as in:

Code:
xml_token '/Coolant Temp/ || /Cabinet Temp/' ~me/cooling.xml

I could make the list of search patterns as many as I needed, certainly more than 2. More like a dozen or so search strings (or patterns as they are here). My attempts have failed, ideas?

Last edited by harleyvrodred; 2 Weeks Ago at 10:18 AM.. Reason: add output script produces
    #11  
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: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
I don't understand. Are you saying that you want to search for a bunch of patterns and return the last element in the XML file that matched any one of those patterns? So, if we look at the last couple of entries in your sample:

Code:
                  <parameter name="Cabinet Temp"  unit="0.1 C">                                                                                                                  
                     <value size="1"  starttime="06-08-2017 13:46:29.751">360</value>                                                                                                                                                                                                                                                                                                                              
                  </parameter>                                                                                                                                                                                                                                                                    
                  <parameter name="Electronics PID Control Value"  unit="-">                                                                                    
                     <value size="1"  starttime="06-08-2017 13:46:42.593">1667</value> 
                  </parameter>

and you decide to search for Cabinet Temp and Electronics PID Control Value, you want the output to be:

Code:
{"Electronics PID Control Value" 0 }

Note that the 0 comes from multiplying 1667 times the string "-" and the space at the end is because there is no second value after a space in unit="-" like there was in your earlier example with unit="0.1 C".

If we had a clear definition of what you're trying to do, we might be able to help you get there. But, with your current description, I'm not able to guess at the output you hope to achieve.
Sponsored Links
    #12  
Old Unix and Linux 2 Weeks 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
In the case you mentioned, I'd prefer to see

Code:
{"Electronics PID Control Value" 1667 -}

I tried adding a few more entries to the example, namely Cabinet Temp - would also have the same fields as Coolant Temp. The real-life file has many Temp related entries that I'm interested in. I could use what is here now and use the xml_token function individually with multiple calls for each one, getting the last entry each. I was wondering if there would be a way to use a search string with multiple entries at the same time so it would return the last entry of each.

I know the subset that I’m interested in.

Another approach might be to take each <parameter> name and return the last instance of each. This help?

I'm data mining. Pulling data from site machines for analysis. There is too much to pull it all. I'm just choosing and picking

Last edited by harleyvrodred; 2 Weeks Ago at 11:55 PM..
Sponsored Links
    #13  
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: 26 June 2017, 8:37 PM EDT
Location: San Jose, CA, USA
Posts: 10,396
Thanks: 527
Thanked 3,627 Times in 3,093 Posts
Quote:
Originally Posted by harleyvrodred View Post
I tried adding a few more entries to the example, namely Cabinet Temp - would also have the same fields as Coolant Temp. The real-life file has many Temp related entries that I'm interested in. I could use what is here now and use the xml_token function individually with multiple calls for each one, getting the last entry each. I was wondering if there would be a way to use a search string with multiple entries at the same time so it would return the last entry of each.

I know the subset that Im interested in.

Another approach might be to take each <parameter> name and return the last instance of each. This help?

I'm data mining. Pulling data from site machines for analysis
We are very glad you know the subset of entries you're interested in. But my crystal ball isn't showing me what is inside your head. What would help would be for us to know the subset of entries you're interested in. Or, if you want the code to determine which entries it should extract, explain to us how you would determine that an entry is interesting by describing what you see on the first line of that <parameter>...</parameter> XML tag that makes it interesting. (Is it that the last word inside the 1st pair of double-quotes is Temp? Is it that string between the 2nd pair of double-quotes is 0.1 C? If it isn't one of these, what is it?)

After you describe the logic that determines which entries are interesting, please show us the exact output that you want your script to produce given the sample you provided in post #4 in this thread. Or post new data in a new post (with CODE tags) and show us the exact output (also in CODE tags) you're hoping to produce from that input with the list of interesting tag values or the logic that you described to determine which tags are interesting.
Sponsored Links
    #14  
Old Unix and Linux 2 Weeks Ago
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 June 2017, 6:17 PM EDT
Posts: 1,633
Thanks: 46
Thanked 622 Times in 583 Posts
Quote:
Originally Posted by harleyvrodred View Post
In the case you mentioned, I'd prefer to see

Code:
{"Electronics PID Control Value" 1667 -}

I tried adding a few more entries to the example, namely Cabinet Temp - would also have the same fields as Coolant Temp. The real-life file has many Temp related entries that I'm interested in. I could use what is here now and use the xml_token function individually with multiple calls for each one, getting the last entry each. I was wondering if there would be a way to use a search string with multiple entries at the same time so it would return the last entry of each.

I know the subset that Im interested in.

Another approach might be to take each <parameter> name and return the last instance of each. This help?

I'm data mining. Pulling data from site machines for analysis. There is too much to pull it all. I'm just choosing and picking
Run as perl example.pl harleyvrodred.example

Code:
my %tmp;
my %parameters;
while(<>){
  if(/<parameter name/../<\/parameter/){
    /(name)="(\w[^"]+)/ and $tmp{$1} = $2;
    /(unit)="([^"]+)/ and $tmp{$1} = $2;
    /(value)[^>]+>(\d+)</ and $tmp{$1} = $2;

    if(/<\/parameter/) {
      $parameters{$tmp{'name'}} = {'unit' => $tmp{'unit'}, 'value' => $tmp{'value'}};
      undef %tmp;
    }
  }
}
for my $entry (keys %parameters) {
  my @param = @{${parameters}{$entry}}{qw(value unit)};
  print qq/{"$entry" @param}\n/;
}

Output:

Code:
{"Electronics PID Control Value" 1667 -}
{"Coolant Temp" 307 0.1 C}
{"Cabinet Temp" 360 0.1 C}
{"Actual Air Velocity" 869 fpm}

The Following User Says Thank You to Aia For This Useful Post:
harleyvrodred (2 Weeks Ago)
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 06:18 AM.