I am writing a bash script to do some parsing on a log and I am running into a problem when it comes to converting only certain sections of the file from hex to ascii or hex to decimal.
Data Example:
The hex values after Hardware and SW Version I need to convert from Hex to ASCII and the Errors, Full, Test values I need to convert from hex to decimal.
Thanks , but how would I go about searching for the line and then converting whatever is in between the brackets because its not going to be the same values every time.
Perhaps id have to search for Hardware and then take whatever is in between the brackets set as a variable and then use the above code for the conversion?
Exactly. And you will need to ensure that you have exactly one space before each hex value for the hex to ascii conversion. And no spaces at all for the hex to decimal conversion.
#!/usr/bin/env ruby
File.open("file").each do|line|
if line =~ /^(SW|Hardware)|Error|Test|Full/i
toconv = line.scan(/\[(.*?)\]/)[0][0]
if line =~ /SW|Hardware/
toconv = toconv.split.map{|x|x.hex.chr}.join
else
toconv = toconv.split.map{|x|x.hex}.join
end
line.gsub!( /(.*\[)(.*)(\].*)/ , "\\1#{toconv}\\3" )
end
puts line
end
Thanks but I dont have ruby installed so I cant test that code out.
trying to finish it up with awk and sed atm, will post if I can get it working.
---------- Post updated at 10:38 AM ---------- Previous update was at 09:57 AM ----------
Awesome vgersh99! I tweaked a few things to match my actual log file and it works perfectly.
Im not to familiar with using awk with bash scripts though, how do I go about combining that piece of awk code with the rest of my bash script? I keep getting errors when I put awk inside the script.
Ok so new problem. Everything was looking ok when I output the data on the screen, But once I actual open or view the output file I get ^@ after every ASCII character. Any ideas? I tried removing with sed and tr but no luck. tr just reverts it back to its hex form