4

I've noticed that lshw and lsof both take minute to load its output, and if I pipe their output to be processed by awk, I get the help message from awk and then . . . nothing from the first command.

What needs to be done to let awk actually extract the output I need ? Is there a way to let pipe hold on until there is output actually available for awk to work on ?

Bellow is what I attempted

$ lsof | awk `{print $3}`                                              
mksh: {print: not found
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:      GNU long options: (standard)
    -f progfile     --file=progfile
    -F fs           --field-separator=fs
    -v var=val      --assign=var=val
Short options:      GNU long options: (extensions)
    -b          --characters-as-bytes
    -c          --traditional
    -C          --copyright
    -d[file]        --dump-variables[=file]
    -e 'program-text'   --source='program-text'
    -E file         --exec=file
    -g          --gen-pot
    -h          --help
    -L [fatal]      --lint[=fatal]
    -n          --non-decimal-data
    -N          --use-lc-numeric
    -O          --optimize
    -p[file]        --profile[=file]
    -P          --posix
    -r          --re-interval
    -S          --sandbox
    -t          --lint-old
    -V          --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
    gawk '{ sum += $1 }; END { print sum }' file
    gawk -F: '{ print $1 }' /etc/passwd

Note that $3 filed is just example, it has same effect with any field. I've also been trying sudo lsof | awk/test.txt/ {print $3}`

I also tried sudo lshw | awk/cpu/ {print }`

NOTE: I know I can use grep, but I am trying to understand here why the short pause that lshw and lsof takes doesn't let awk do its job, and how can I work around that pause the two commands take

6
  • Please show the command you're running, and the output. May 1, 2015 at 20:24
  • Piping should be a blocking operation..what command were you trying ?
    – heemayl
    May 1, 2015 at 20:24
  • Use proper quoting, your command is being interpreted by mksh
    – heemayl
    May 1, 2015 at 20:40
  • @heemayl could you explain that in a bit more detail ? May 1, 2015 at 20:46
  • 1
    Ooooh, I see it now ! I was using an accent instead of single quote ! Facepalm May 1, 2015 at 20:55

1 Answer 1

3

The problem was all about using proper quotes.

In your command

lsof | awk `{print $3}` 

you are giving the awk action pattern inside command substitution syntax (back quotes), as a result your shell mksh is treating it as a command and giving this error while substituting it:

mksh: {print: not found

As you have already got this, you need proper quoting:

lsof | awk '{print $3}'
2
  • now I kind of feel stupid. My attention suffers today - probably not enough coffee =) Thank you, though. I was getting utterly confused May 1, 2015 at 21:21
  • 1
    @Serg Come on..we all experience these kind of moments every now and then :)
    – heemayl
    May 1, 2015 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .