0

I have a logfile with multiple lines like "DEBUG MDM payload:" and on separate lines after that is the xml content which can differ in lenght but always ends the same. How can i user tr and sed or another method to combine all the xml content on the same line as "DEBUG MDM payload:"?

2017-01-26T10:54:28.712+0100 - CORE {wff-device-thread-15 : deviceRestExecuteWorkflow.deviceRestExecuteActivity.EventQueueConsumer} device.ios.management|logger [{{Correlation,dhdjwdw-3a44-4b0d-aa52-dwdwdwdwdw}{Uri,PUT /S29112264/ios/mdm2/dwdwdwdw-c7d2-465c-be44-dwdwdwdw}{host,de0-server.name.fqdn}}] - DEBUG MDM payload:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Command</key>
<string>dd2c2a59-2007-4863-9c8c-dgfhgfh666666</string>
<key>command</key>
<dict/>
<key>Status</key>
<string>Approved</string>
<key>UDID</key>
<string>90f0c56287474782748274827487842cc64c2fc4</string>
</dict>
</plist>

Update: The following command will print what i want just cant figure out how to take that and alter the file to put it on one line.

sed -n "/\/ios\/mdm2\DEBUG MDM payload/,/<\/plist>/p"

Update 2: Ok bit further now but i still have some tabs in some cases. trying to find a way to remove tabs next.

sed -n "/DEBUG MDM payload/,/<\/plist>/p" fake_log.txt | tr -d "\r" | tr -d "\n"

Update 3: OK, got it. Now is there a way to remove the existing lines and have this newly modified line added in its place?

sed -n "/DEBUG MDM payload/,/<\/plist>/p" fake_log.txt | tr -d "\r" | tr -d "\n" | tr -d "\t"
miken32
  • 42,008
  • 16
  • 111
  • 154
rcmpayne
  • 163
  • 2
  • 15

1 Answers1

0

To replace newlines in place, sed is probably not the tool you're looking for. Try using Perl instead. This one-liner should do the trick.

perl -i -a -n -e '$matched=true if /DEBUG MDM payload/; if ($matched && /<\/plist>/) {print "@F\n"; $matched=false;} elsif ($matched) {print @F;} else {print "@F\n";}' log.txt

To do a dry run, just remove the -i option, and it will output instead of changing the file in place.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • sort of working but i had to add a second one for \r. Now i just need to tr -d tabs as well. sed -n "/DEBUG MDM payload/,/<\/plist>/p" fake_log.txt | tr -d "\r" | tr -d "\n" – rcmpayne Feb 02 '17 at 23:04
  • If you'd like to change this in-place, you're looking at the wrong tools for the job. See discussion here: http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed – miken32 Feb 02 '17 at 23:20