Questions tagged [cat]

The cat command is a standard Unix program used to concatenate and display files. The name is from catenate, a synonym of concatenate.

cat - concatenate files and print on the standard output.

The cat command is used for:

  1. Display text file on screen
  2. Read text file
  3. Create a new text file
  4. File concatenation
  5. Modifying file

Example

$ cat -n file #shows the content of a file, including the line-numbers(-n flag)

Concatenating two files into third file:

cat file1 file2 > output_file 

The purpose of cat is to concatenate (or catenate) files. If it is only one file, concatenating it with nothing at all is a waste of time, and costs you a process." This is also referred to as "cat abuse". Nevertheless the following usage is common:

cat filename | command arg1 arg2 argn
cat -- "$file" | grep -- "$pattern"
cat -- "$file" | less

can instead be written as:

grep -- "$pattern" "$file"
less "$file"

A common interactive use of cat for a single file is to output the content of a file to standard output. If the output is piped or redirected, cat is unnecessary.

Without two named files, the use of cat has no significant benefits.

Useful links

1519 questions
0
votes
1 answer

Piping and redirecting with cat

Looking over the Dokku source code, I noticed two uses of pipe and redirect that I am not familiar with. One is: cat | command Example: id=$(cat | docker run -i -a stdin progrium/buildstep /bin/bash -c "mkdir -p /app && tar -xC /app") The other is…
ustun
  • 6,941
  • 5
  • 44
  • 57
0
votes
2 answers

linux one liner, show name of files matching a pattern

Let us say I have a list of files with extension .txt. And I'd like to know what is the name of the files that match a regex pattern, how would I do that in linux. Right now I am doing: cat *.txt | grep -f list.txt where list.txt contains a list of…
Dnaiel
  • 7,622
  • 23
  • 67
  • 126
0
votes
1 answer

Significance of echo Start|cat>>log in bash

Can anyone help me out with the following code snippet - echo Start|cat>>log When I tried echo Start>>log it gave the same output to the log file. Can anyone explain the difference between the two commands?
nsane
  • 1,715
  • 4
  • 21
  • 31
0
votes
3 answers

Exporting string in bash

I would like to export an encrypted string to a file that already has some content. How can i delete the content of this file and add my string? I tried cat but that did not work. Thanks for your help!
WorstCase
  • 25
  • 1
  • 1
  • 4
0
votes
2 answers

Different show between "cat" and "vim"

cat /tmp/test: >> cat /tmp/test Hello World Hello World Hello World Hello World vim /tmp/test: >> vim /tmp/test ==> 1 ^[[1;31mHello World^[[0m 2 ^[[2;32mHello World^[[0m 3 ^[[5;33mHello World^[[0m 4 ^[[6;34mHello World^[[0m Sorry , I have google…
iceKing
  • 147
  • 3
  • 13
0
votes
3 answers

executing variable with eval '$cmd' will generate error

If I save my cat command into a string and then I execute it then I will get an error linux# cmd="cat /data/test/test.tx* | grep toto" linux# eval '$cmd' cat: |: No such file or directory cat: grep: No such file or directory cat: toto: No such file…
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0
votes
1 answer

Getting Cat ID when product is in multiple Cats magento

Only problem I have : I want to make a link on the product page :/public_html/app/design/frontend/default/default/template/catalog/product/view.phtml to the Category the products belong. I can get this gixed by Problem ; Most of products are in…
0
votes
3 answers

replace string after match

I have a bunch of files look like this: 1x 4b 2x 4b 3x 4b .subckt test xxx 1t 4b 2t 4b 2x 4b So my question is that how can I replace the 4b after ".subckt test xxx" with 8b and overwrite the original file. And recursively do it for all files in…
0
votes
4 answers

How do i copy every line X line from a bunch of files to another file?

So my problem is as follows: I have a bunch of files and i need only the information from a certain line in each of these files (the same line for all files). Example: I want the content of the line 10 from file example_1.dat~example_10.dat and then…
0
votes
3 answers

How to append one file to the other, with first file being edited, hence can't use usual cat command

Suppose that I have two files, each of them have header in the first line and records in the remaining lines. And I want to concatenate two files into one, but don't include header twice. I tried the following commands while googling for the answer,…
Blaszard
  • 30,954
  • 51
  • 153
  • 233
0
votes
3 answers

command line "cat >> " behavior

What does this command do cat t.txt >> t.txt ? let say the t.txt only have one line of text "abc123". I assume the output of "abc123" is appended to t.txt. So, I should have 2 lines of "abc123". However, it just going in to a infinite loop. It…
Billz
  • 7,879
  • 7
  • 33
  • 35
0
votes
1 answer

find + xargs + cat does not work with --name argument

I'm trying to concatenate all the files of a that ends with coref extension. This works (but add unwanted files): find ../corpus/dev/txt/ | xargs cat This not works. find ../corpus/dev/txt/ -name '*.coref' | xargs cat In the second comand find…
Nasgar
  • 859
  • 2
  • 11
  • 26
0
votes
1 answer

Dealing with cat and head in a bash loop

I am trying to add two lines of text from one file to the beginning of a bunch of other files by using a for loop in the bash command line. I tried this in the command line: $for i in *.txt; do head -2 ../../041_R_def_c.txt > X; cat i >> X; \mv X i;…
0
votes
3 answers

How to read a file AND make a query at each line?

I have a file .csv and would like to add information to it. The delimiter is ; . I had an idea to do so but it didn't really work. It was : cat file.csv | awk -F ";" '{ print ""$1" "$2" "$3 }' > temp.csv cat temp.csv | while read…
oz380
  • 57
  • 3
  • 9
0
votes
1 answer

How can I cat back exact formatting regardless of shell?

While trying to write a script, I found an interesting issue with cat today. If I do the following at the command line, everything works properly: var=$(ssh user@server "cat /directory/myfile.sh") echo $var > ~/newfile.sh This works and I have a…
nullByteMe
  • 6,141
  • 13
  • 62
  • 99