-1

The output of command line utility ./Utility -P consist of two rows of text. I use shell script to capture standard output from this sub shell and show it as notification use zenity:

#!/bin/bash
output="$( ./Utility -P )"
zenity --info --text "${output}" --title "Job done"

The only the second row matters, so I need show only 2nd row from the utility output. Now I can pipe only 2nd row for further processing in above shell script?

I'm considering use awk sample: awk -F: 'NR==2 {print $1}' filename

How to connect this sample to above shell code?

11
  • 2
    Why awk -F: 'NR==2 {print $1}' filename? If you want the whole second line, awk 'NR==2 {print}' filename => output=$(./Utility -P | awk 'NR==2 {print}'), however for this task you could use just sed: output=$(./Utility -P | sed -n '2p')
    – kos
    Commented Jun 11 at 21:54
  • Yes, both examples works. Probably, the double quotes be used around the expression? output="$( ./Utility -P | awk 'NR==2 {print}')"
    – Lexx Luxx
    Commented Jun 11 at 22:31
  • 1
    No, you can leave them out, word splitting and globbing aren't performed on assignments such as var=something: unix.stackexchange.com/a/599170/114435
    – kos
    Commented Jun 11 at 22:39
  • 1
    Then by all means use sed: output=$(./Utility -P | sed -En '2s/.*?(.{10})$/\1/p') (this will remove everything but the last 10 characters on line 2, regardless of the characters, I think it's ok? Otherwise if you want to avoid printing in case one of the last ten characters is something other than a letter or a digit: output=$(./Utility -P | sed -En '2s/.*?([A-Za-z0-9]{10})$/\1/p') )
    – kos
    Commented Jun 11 at 23:50
  • 1
    Ah I think I know. What's this Utility thing? Is it printing using Windows-style line-endings (\r\n)? Try ./Utility -P | tr -d '\r' | sed -En '2s/.*?(.{10})$/\1/p'. If it works, you have carriage returns in your line endings.
    – kos
    Commented Jun 12 at 0:36

0

You must log in to answer this question.

Browse other questions tagged .