New answers tagged

3

Let's decompose both commands num_rec=`cat test.csv |wc -l|tr -d " " | nawk '{printf("%0.6d\n", $1) }'` is a compounded command : A | B | C | D A: cat test.csv just cats the file test.csv (and is also a UUOC, "Useless Use Of Cat") B: wc -l will count the number of lines from its input (stdin), which is here test.csv's content. So it will count the ...


0

They're commands written by a novice who either hasn't learnt anything since the early 90s or is following a HOWTO from that era. Would be better written as: num_rec=$(wc -l <test.csv) num_rec=$(printf "%0.6d" $(( num_rec + 2 )) ) This uses command substitution to get the line count from test.cv using wc -l (by redirection, so that wc doesn't print ...


1

If what you want to do is transfer 2 files with only 1 connection, you will have to somehow use a marker to separate the files in the data stream, as end-of-file is a read of length 0 that netcat will use to close the socket. Control-D may be what you can type in a terminal to signify end-of-file, but it does not generate a character, it terminates the read ...


0

You really shouldn't be 'cat'ing arbitrary files. It's a bad idea even if the file is not malicious. You can determine the file 'type' by running the 'file' program, e.g. file /usr/bin/vi. Using 'cat' is kind of low-level anyway. Perhaps try using 'more' or 'less'. When I try more /usr/bin/vi I get /usr/bin/vi: Not a text file. Also, you can reset your ...


2

You could do cat -v /usr/bin/vi to have it print the unprintable characters as ASCII representations, not as actual control characters that the terminal may try to process. Does that solve your problem?


3

Why not simply use the proper commands: echo "<?php" && cat test.php


0

The problem is that null bytes cannot be passed through command line arguments as they are internally used as argument terminators. All other bytes seem to be fine. So a somewhat more space-efficient (typically) alternative to using base64 would be to escape the null bytes and then use printf to convert the data to original form: pngString="$(sed ...


0

Just too brief. tee can be used for this purpose ps | tee ps-info.txt


2

tee writes both to file and stdout , use that: ps -ef | tee psoutput.txt | less Or just use && to open file with less upon successful completion of ps ps -ef > psoutput.txt && less psoutput.txt


3

Here's an awk script that stores the whole file in memory: awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' file Phrased as a shell function: tac () { awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' "$@"; }


1

Here's a clean clear-cut POSIX solution for on-disk files: #!/bin/sh function tac () { lines=$(wc -l < "$1") while [ $lines -gt 0 ] do head -n $lines "$1" | tail -n 1 lines=$((lines-1)) done } The main down-side is that it reads the file once for every line in the file. POSIX doesn't specify an upper limit for -n number, so large files ...


2

You can do this with a sed one-liner as follows, though it is certainly not readable for the "uninitiated": sed -n '1h;1!{x;H;};${g;p;}' file.txt Explanation: -n suppresses sed's default action of printing each line. 1h causes the first line to be stored in the hold space. 1!{...} applies this block of commands to all lines except for the first one. ...


8

Bear in mind that dd is a raw interface to the read(), write() and lseek() system call. You can only use it reliably to extract chunks of data off regular files, block devices and some character devices (like /dev/urandom), that is files for which read(buf, size) is guaranteed to return size as long as the end of the file is not reached. For pipes, sockets ...


3

With a bashism, and a functionally "useless use of cat", but closest to the syntax the OP uses: cat <(dd if=file1 bs=1M count=99 skip=1) \ <(dd if=file2 bs=1M count=10) \ > final_output (That being said, Stephen Kitt's answer seems to be the most efficient possible method.)


18

dd can write to stdout too. ( dd if=file1 bs=1M count=99 skip=1 dd if=file2 bs=1M count=10 ) > final_output


10

I don't think you can easily read multiple files in a single dd invocation, but you can append to build the output file in several steps: dd if=file1 bs=1M count=99 skip=1 of=final_output dd if=file2 bs=1M count=10 of=final_output oflag=append conv=notrunc You need to specify both conv=notrunc and oflag=append. The first avoids truncating the output, the ...



Top 50 recent answers are included