Thanks for looking at this.

I have a possible simple question, but I wasn't able to find any help so far on the internet.

I have a script that has mainly 2 commands that process output. But both commands must end up on a single line in a text file that my Nagios system reads and displays. Nagios won't accept the 2nd line to print in the web interface, so I need to have them on 1 line.

The commands. first a simple print of a file name:

ls -tr | tail -n1 > /tmp/output.file

this will print the newest file in the folder and writes the name of the file in /tmp/output.file for example: bla-1357135486.xml

The 2nd command is a bit more complex. but finds a field inside that file. and prints that.

head -n3 bla-1357135486.xml |  awk -F 'scheduleName' '{ print $2 }' | sed -r 's/^.{1}//' | sed -r 's/.{1}$//'

The output of this command look like this:

"ChannelName" "scheduleStart=<date>" "scheduleEnd=<date>"

I want to have the line in the output.file to look like this:

bla-1357135486.xml "ChannelName" "scheduleStart=<date>" "scheduleEnd=<date>"

What to add to get it to work.


Maybe by storing the output in variables and writing these variables to the file?

output1=`ls -tr | tail -n1`
output2=`head -n3 bla-1357135486.xml |  awk -F 'scheduleName' '{ print $2 }' | sed -r 's/^.{1}//' | sed -r 's/.{1}$//'`

echo "$output1 $output2" > /tmp/output.file
  • 1
    Why didn't I think of that :) Thanks. That is a good solution... – SHLelieveld Feb 25 '15 at 12:56

If you use some text editor try to use just one:

awk -F ".scheduleName." '
    NR==1{printf "%s ", FILENAME}
    NR<4{printf "%s ",$2}
    END{print""}' $(ls -t | head -1)

I hope is there more than 1 scheduleName in line to remove first and last symbols from second field?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.