Live data from Hacker News

Useful use of cat(1)

in-ulm.de

41–46 of 46 posts

Re: Useful use of cat(1)

#41
post #10

ps has a nasty habit of truncating its output. Piping it into cat is a way around this. E.G. $ echo $COLUMNS 118 $ ps -fp 14706 UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshuttle/main.py pytho $ ps -fp 14706 | cat UID PID PPID C STIME TTY TIME CMD root 14706 14705 0 Jan29 pts/3 00:00:00 sudo -p [local sudo] Password: python /usr/lib/sshutt…

Using the -ww flag will produce unlimited width output

Thanks! I'd searched the ps manpage for truncate and length. I don't know why I didn't think to look for width.

Re: Useful use of cat(1)

#42
post #38

Also surprisingly useful in command line video editing. For instance, you can join mpg files together... cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f

Well, that's the main purpose of cat - concatenate files and print on the standard output.

Re: Useful use of cat(1)

#43
post #18

Earlier quoted context omitted.

No, that just concatenates a file reading bytes from the end, backwards. The real opposite of cat would be a putative 'trunc'. So if you typed $ cat somefile It would print the file to your terminal. And then if you said $ trunc somefile The printed lines would be removed from your terminal. :)

In case you missed it, "tac" is literally the reverse of "cat" . As in "cat".reverse() or something.

I get that tac exists and is useful for concatenating and printing files in reverse.

Neilk was just humorously pointing out that since cat stands for concatenate, its reverse should be truncate, and since cat is often abused to print a single file to a terminal this fictional trunc should "unprint" it.

Arguably you could view 'comm -3' as the reverse of cat.

  # output is all the lines of file1 and file2
  cat file1 file2

  # output is the lines in file1 not in file 2, and vice verse
  comm -3 file1 file2

Re: Useful use of cat(1)

#44
post #43

Earlier quoted context omitted.

In case you missed it, "tac" is literally the reverse of "cat" . As in "cat".reverse() or something.

I get that tac exists and is useful for concatenating and printing files in reverse. Neilk was just humorously pointing out that since cat stands for concatenate, its reverse should be truncate, and since cat is often abused to print a single file to a terminal this fictional trunc should "unprint" it. Arguably you could view 'comm -3' as the reverse of cat. # output is all the lines of file1 and file2 cat file1 file…

> cat stands for concatenate

I almost resisted the urge, but no, the pedant in me :-) insists on pointing out that cat is short for catenate

Re: Useful use of cat(1)

#46
The "convert file into arguments" trick is useful, but a "while read/do" loop would be better:

    cat file | while read INPUT
    do
        command "$INPUT"
    done
... which will read the entire line from 'file' into the shell variable "$INPUT", inclusive of whitespace. If you need to do further parsing, of course, you can do that within the loop.

Trap: since you're creating a subshell (at least in bash/Bourne), variables defined within the loop are NOT defined after it exits.

Post reply on HN