Live data from Hacker News

Linux Commands for Developers

blog.jayfields.com

21–30 of 67 posts

Re: Linux Commands for Developers

#21
One I learned for the first time the other day is 'paste'. Good for people like me who never fully grokked awk, it joins lines from separate files into a single file.

Say you have two files, one with lines of numbers:

  1
  2
  3
... and one with letters:

  A
  B
  C
$ paste numbers letters

  1	A
  2	B
  3	C
Want CSV?

$ paste -d, numbers letters

  1,A
  2,B
  3,C
Or, with '-s' you can join lines from inside the same file. For instance, you can sum numbers:

$ paste -sd+ numbers

  1+2+3
$ paste -sd+ numbers |bc

  6
(Thanks to a Stack Overflow post somewhere for suggesting that one!)

Useful example: the total resident memory size of all chromium processes:

$ ps --no-headers -o rss -C chromium | paste -sd+ | bc

  793180

Re: Linux Commands for Developers

#22
post #18

how do I execute the out put of something like this: grep -i 'pattern' file | awk '{print $5}' | sed 's/^/cmd/g' I end up sending to a file, chmod, then run it at the shell.

In bash...

    $(grep -i 'pattern' file | awk '{print $5}' | sed 's/^/cmd/g')
?

Or surround in backticks

    `command which outputs text you want to run as a command`
I prefer $() as they nest better.

Or have I misunderstood your question?

Re: Linux Commands for Developers

#23
post #8
post #6

I was expecting to see the first comment in here complaining about his use of 'cat', as in all of the examples his second argument could've easily taken a filename argument.. sort order.* is surely more elegant than cat order.* | sort which is fair enough, however, as it happens, i generally do end up using 'cat' in the way he's used it.. for such small jobs nobody can be genuinely worried about the overhead, and it…

Indeed, I don't see why people get so upset (or pedantic) about what are, effectively, NOPs in command-lines. However, things change if you start adding certain options to sort:- sort -m order.* and cat order.* | sort -m are definitely not the same thing (for most input files at least).

Perhaps because sending GiBs through read(2) and write(2) unnecessarily isn't a NOP?

Re: Linux Commands for Developers

#24
post #6

I was expecting to see the first comment in here complaining about his use of 'cat', as in all of the examples his second argument could've easily taken a filename argument.. sort order.* is surely more elegant than cat order.* | sort which is fair enough, however, as it happens, i generally do end up using 'cat' in the way he's used it.. for such small jobs nobody can be genuinely worried about the overhead, and it…

If you really prefer to see things in order then

    
is an alternative to

    cat foo | sort | ...
though I wouldn't particularly recommend it. Instead the overhead of cat(1) should be omitted and it written in the normally accepted form of

    sort foo | ...
Providing a filename rather than re-directing stdin allows the program more choice over its method of access.

Re: Linux Commands for Developers

#25
post #6

I was expecting to see the first comment in here complaining about his use of 'cat', as in all of the examples his second argument could've easily taken a filename argument.. sort order.* is surely more elegant than cat order.* | sort which is fair enough, however, as it happens, i generally do end up using 'cat' in the way he's used it.. for such small jobs nobody can be genuinely worried about the overhead, and it…

Minor nitpick from the man page: "egrep is the same as grep -E. fgrep is the same as grep -F. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified."

Re: Linux Commands for Developers

#26
post #20
post #18

how do I execute the out put of something like this: grep -i 'pattern' file | awk '{print $5}' | sed 's/^/cmd/g' I end up sending to a file, chmod, then run it at the shell.

Wrap it in $() to execute. And even if you pipe it into a file, no chmod is required. "sh file" works as it should

or use eval

~$ help eval eval: eval [arg ...] Execute arguments as a shell command.

    Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.
   Exit Status:
    Returns exit status of command or success if command is null.

Re: Linux Commands for Developers

#27

I came across something in fortune some time ago which you can find at: http://motd.ambians.com/quotes.php/name/linux_songs_poems/to... I have found it to be surprisingly useful. Nobody uses all the commands but remembering that something like zcat exists can be extremely useful. Also remembering to pipe through sort before sending through uniq is helpful as well.

In most cases you can replace "sort | uniq" with "sort -u".

Re: Linux Commands for Developers

#28
post #23
post #8

Earlier quoted context omitted.

Indeed, I don't see why people get so upset (or pedantic) about what are, effectively, NOPs in command-lines. However, things change if you start adding certain options to sort:- sort -m order.* and cat order.* | sort -m are definitely not the same thing (for most input files at least).

Perhaps because sending GiBs through read(2) and write(2) unnecessarily isn't a NOP?

You can do that sending while you're waiting for the disk to provide said GiBs. I also believe that useless uses of cat are often acceptable for readability (many novices are not familiar with redirecting standard input, particularly not as the first thing on a command line).

Re: Linux Commands for Developers

#29
post #21

One I learned for the first time the other day is 'paste'. Good for people like me who never fully grokked awk, it joins lines from separate files into a single file. Say you have two files, one with lines of numbers: 1 2 3 ... and one with letters: A B C $ paste numbers letters 1 A 2 B 3 C Want CSV? $ paste -d, numbers letters 1,A 2,B 3,C Or, with '-s' you can join lines from inside the same file. For instance, you…

`paste` is also very useful together with stdin redirection and subshells. E.g.:

    paste 

Re: Linux Commands for Developers

#30
post #23
post #8

Earlier quoted context omitted.

Indeed, I don't see why people get so upset (or pedantic) about what are, effectively, NOPs in command-lines. However, things change if you start adding certain options to sort:- sort -m order.* and cat order.* | sort -m are definitely not the same thing (for most input files at least).

Perhaps because sending GiBs through read(2) and write(2) unnecessarily isn't a NOP?

Who said NOPs are free? NOPs still take at least one clock cycle.
Post reply on HN