Hot answers tagged

232

Use sed Usage $ cat file Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 To print one line (5) $ sed -n 5p file Line 5 To print multiple lines (5 & 8) $ sed -n -e 5p -e 8p file Line 5 Line 8 To print specific range (5 - 8) $ sed -n 5,8p file Line 5 Line 6 Line 7 Line 8 To print range with other specific line (5 - 8 & ...


165

There are multiple ways to do this. The simplest is probably this: cat <<EOF | sh touch somefile echo foo > somefile EOF Another, which is nicer syntax in my opinion: ( cat <<EOF touch somefile echo foo > somefile EOF ) | sh This works as well, but without the subshell: { cat <<EOF touch somefile echo foo > somefile EOF } | ...


121

Try it with -f or --force: zcat -f -- * Since zcat is just a simple script that runs exec gzip -cd "$@" with long options that would translate to exec gzip --stdout --decompress "$@" and, as per the man gzip (emphasize mine): -f --force Force compression or decompression even if the file has multiple links or the corresponding file already ...


110

In the first case, cat opens the file, and in the second case, the shell opens the file, passing it as cat's standard input. Technically, they could have different effects. For instance, it would be possible to have a shell implementation that was more (or less) privileged than the cat program. For that scenario, one might fail to open the file, while the ...


94

head -c 50 file This returns the first 50 bytes. Mind that the command is not always implemented the same on all OS. On Linux and macOS it behaves this way. On Solaris (11) you need to use the gnu version in /usr/gnu/bin/


80

On OS/X like on many systems (BSDs, Solaris, AIX, IRIX...), the functionality of GNU tac is available in tail with the -r option. So no need to install GNU tac: tail -r the-file


78

If the file(s) in question contain really lots of data sending the signal can actually get to cat before it finishes. What you really observe is the finite speed of your terminal - cat sends the data to the terminal and it takes some time for the terminal to display all of it. Remember, that usually it has to somehow redraw the whole output window for each ...


72

Yes: Install Homebrew brew install coreutils ln -s /usr/local/bin/gtac /usr/local/bin/tac apparently not needed with latest Homebrew, see comment by Ran Ever-Hadani below or use MacPorts to install coreutils in a similar way.


71

All of the following commands are equivalent. They read the bytes of the CD /dev/sr0 and write them to a file called image.iso. cat /dev/sr0 >image.iso cat </dev/sr0 >image.iso tee </dev/sr0 >image.iso dd </dev/sr0 >image.iso dd if=/dev/cdrom of=image.iso pv </dev/sr0 >image.iso cp /dev/sr0 image.iso tail -c +1 /dev/sr0 >image....


60

var=$( cat foo.txt ) would store the output of the cat in variable var. var=$( ./myscript ) would store the output of myscript in the same variable.


59

type tells you what the shell would use. For example: $ type echo echo is a shell builtin $ type /bin/echo /bin/echo is /bin/echo That means that if, at the bash prompt, you type echo, you will get the built-in. If you specify the path, as in /bin/echo, you will get the external command. which, by contrast is an external program that has no special ...


59

cat < file1 > file2 is not a UUOC. Classically, < and > do redirections which correspond to file descriptor duplications at the system level. File descriptor duplications by themselves don’t do a thing (well, > redirections open with O_TRUNC, so to be accurate, output redirections do truncate the output file). Don’t let the < > symbols ...


53

The most obvious one is cat. But, also have a look at head and tail. There are also other shell utillities to print a file line by line: sed, awk, grep. But those are to alternate the file content or to search inside the file. I made a few tests to estimate which is the most effective one. I run all trough strace to see which made the least system calls. My ...


51

Using find, sort and xargs: find . -maxdepth 1 -type f -name 'file_*.pdb' -print0 | sort -zV | xargs -0 cat >all.pdb The find command finds all relevant files, then prints their pathnames out to sort that does a "version sort" to get them in the right order (if the numbers in the filenames had been zero-filled to a fixed width we would not have needed -...


46

try: find . -type f -exec cat {} +


46

cat is hashed (/bin/cat) is just like cat is /bin/cat (that is, it's an external program). The difference is that you already ran cat in this session, so bash has already looked it up in $PATH and stored the resulting location in a hash table so it doesn't have to look it up again in this session. To see all the commands that have been hashed in your ...


45

Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cat's man page: cat - concatenate files and print on the standard output Now cat is fine for printing files but there are alternatives: echo "$(<filename)" or printf "%s" "$(<filename)" The ( ) return the value of an ...


44

zless It seems a pity about zcat, as libz has an API that supports reading from both compressed and uncompressed files transparently. But the manpage does say that zcat is equivalent to gunzip -c.


44

One way of doing it is by using sed: cat -n text.txt | sed '11d' where 11 is the number of the line you want removed. Or to remove all but 11: cat -n text.txt | sed '11!d' Ranges are also possible: cat -n text.txt | sed '9,12!d' And cat -n isn't even needed: sed '9,12!d' text.txt


38

You can do this with find alone using the -exec action: find /location -size 1033c -exec cat {} + {} will be replaced by the files found by find, and + will enable us to read as many arguments as possible per invocation of cat, as cat can take multiple arguments. If your find does not have the standard + extension, or you want to read the files one by one: ...


37

Both of those examples are useless uses of cat. Both are equivalent to wc < file1 > file2. There is no reason to use cat in this example, unless you are using cat file as a temporary stand-in for something that dynamically generates output.


36

Your cut command works if you use a pipe to pass data to it: cat ${file} | cut -c1-50 Or, avoiding a useless use of cat and making it a little safer: cut -c1-50 < "$file" Note that the commands above will print the first 50 characters (or bytes, depending on your cut implementation) of each input line. It should do what you expect if, as you say, ...


36

There are several possibilities, all depending on the exact parameters of your situation right now. I'm going to assume Linux in the following examples where applicable, but similar functionality exists on other platforms in most cases. You might be able to get the dynamic loader to run an executable for you. Assuming cat is dynamically-linked, your ...


35

#!/bin/bash if [[ "$#" -ne 1 ]]; then echo "Usage: $0 [INPUT FILE]" 1>&2 exit 1 fi cat "$1"


35

For people who can run scp, you can do this: scp remotehost:/path/to/remote/file /dev/stdout


34

In Unix, most objects you can read and write - ordinary files, pipes, terminals, raw disk drives - are all made to resemble files. A program like cat reads from its standard input like this: n = read(0, buffer, 512); which asks for 512 bytes. n is the number of bytes actually read, or -1 if there's an error. If you did this repeatedly with an ordinary ...


32

cat file | wc | cat > file2 would usually be two useless uses of cat as that's functionally equivalent to: < file wc > file2 However, there may be a case for: cat file | wc -c over < file wc -c That is to disable the optimisation that many wc implementations do for regular files. For regular files, the number of bytes in the file can be ...


31

less prints text to stdout. stdout goes to a terminal (/dev/tty?) and opens the default buffer viewer through a pipe when piping it to another programm using | (less text | cut -d: -f1) to a file when redirecting it with > (less text > tmp) There is a C function called "isatty" which checks if the output is going to a tty (less 4.81, main.c, line 112). ...


30

Yes it can, with the unimaginatively named cat command: $ cat *csv > all.csv cat does what it says on the bottle, it conCATenates its input and prints to standard output. The command above will give an error if a file called all.csv already exists in the target directory: $ cat *csv > all.csv cat: all.csv: input file is output file You can safely ...


Only top voted, non community-wiki answers of a minimum length are eligible