3

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

  • Please show the command you're running, and the output. – glenn jackman May 1 '15 at 20:24
  • Piping should be a blocking operation..what command were you trying ? – heemayl May 1 '15 at 20:24
  • Use proper quoting, your command is being interpreted by mksh – heemayl May 1 '15 at 20:40
  • @heemayl could you explain that in a bit more detail ? – Sergiy Kolodyazhnyy May 1 '15 at 20:46
  • 1
    Ooooh, I see it now ! I was using an accent instead of single quote ! Facepalm – Sergiy Kolodyazhnyy May 1 '15 at 20:55
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}'
  • now I kind of feel stupid. My attention suffers today - probably not enough coffee =) Thank you, though. I was getting utterly confused – Sergiy Kolodyazhnyy May 1 '15 at 21:21
  • 1
    @Serg Come on..we all experience these kind of moments every now and then :) – heemayl May 1 '15 at 21:26

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.