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

store value of cat to a variable in unix

I need to assign two columns (column 2 and 6) in a text file to a variable as an array. so that i can call them back element by element. the code below puts the whole column as one string element in k and l. and the echo command will not return…
Javad
  • 99
  • 1
  • 2
  • 13
0
votes
2 answers

How to replace multiple blank lines with a single line in C language

I want to squeeze multiple lines in a single line. I have tried to apply my own logic but there is something wrong. char *p; linecount=0; while (fgets(buffer, sizeof(buffer), file)) { …
Ravi Vyas
  • 137
  • 1
  • 4
  • 19
0
votes
2 answers

Redirecting output and then doing cat doesn't work?

When i write the below line in terminal it doesn't work ./a.out < filename.txt | cat filename.txt Pipelining does not work. The process gets terminated after executing ./a.out < filename.txt but doesn't go to cat. Can anyone tell me why is this…
CodeHacker
  • 31
  • 6
0
votes
0 answers

linux add a file to an existing file

I have a bash script that runs through several folders and processes the files' content. The resulting file, newFile I want to append to a file in another folder. This file has the same name (newFile). So, I do this cat $outfn1 >>…
SigneMaten
  • 493
  • 1
  • 6
  • 13
0
votes
1 answer

cat with here document not writing quoted strings to file

I'm writing a shell script that automatically writes a bash script using cat. However, when I run the script, the produced bash script does not include data found in quotation marks. Here's my script: cat < script.bash scriptDir=`dirname…
user1599051
  • 43
  • 1
  • 1
  • 7
0
votes
2 answers

How can I concatenate one file into a list of files which are inside different directories?

I'm working in bash and I'm trying to put one text file at the end of every file in different directories, I have something like BaseFile /Dir1/File1 /Dir2/File2 /DirN/FileN I want to put the text of BaseFile at the end of every File1, File2, ...,…
0
votes
2 answers

How do I cat a file piped from echo?

I would expect this to print the contents of foobar.txt: echo "~/sandbox/foobar.txt" | cat But instead it just prints to the console: ~/sandbox/foobar.txt How can I cat the file contents instead of printing the filename? EDIT: Here's a…
Daryl
  • 3,253
  • 4
  • 29
  • 39
0
votes
1 answer

How to Grep & Cat text files based on an identifier line from a multi-text file

all, I am looking for an efficient way to organize and filter certain types of text files. Let's say I have 10,000,000 text files that are concatenated to larger chunks that are formatted like this @text_file_header ID0001 some…
user2489252
0
votes
3 answers

Avoid newline when combining two variables and outputing to file

I am trying to run such sh script to gather geo information about Ip addresses. inputfile.txt is just a one column file with ip addresses in it. Here is a sample of my input…
jam
  • 65
  • 1
  • 11
0
votes
1 answer

Echo command cuts new lines

I'm trying this command in my prompt to get all results into one file: $ echo $(apt-cache search nano) > search But, they are writing in a one line: $ cat search alpine-pico - Simple text editor from Alpine, a text-based email client…
Alexey Berezuev
  • 785
  • 9
  • 27
0
votes
1 answer

newline after cat multiple txt files

I'm trying to use bash to read data from several one line text files, each in its own directory, and spit them out into one master file, each on a new line. The data of my input file is structured like so ; Store# online backoffice win7 I'm looping…
3030tank
  • 99
  • 2
  • 10
0
votes
2 answers

Perl: Can't call method "catfile" without a package or object reference

I'm trying to use 'cat file' in my Perl module like below, my $batch_dir = '/home/lsubramaniyam/xxx'; my $receive_file = $batch_dir->catfile("Recieve_file.txt"); But, on executing the script, I'm getting error as Can't call method "catfile"…
Logunath
  • 477
  • 1
  • 8
  • 20
0
votes
1 answer

How to use a mixture of cats and pipes in subprocess

I am trying to cat the contents of a file and pipe it into the stdin of a second python script, then put the stdout of that into another file. On the command line it looks something like this: cat input_file | python3 ~/Desktop/python_script.py >…
holmeswatson
  • 969
  • 3
  • 14
  • 39
0
votes
1 answer

writeLines in R with string

I'm trying to create a data file (data.file). I have a string: string=c("test1","test2","test3") which I need to combine with some other commands and write to a text file. string can be of any length. In this specific case, the output I want in…
James
  • 309
  • 1
  • 10
0
votes
1 answer

Using cat : nothing happens

I have this script : #!/bin/bash DIR_TMP=$HOME/.tmp BIB=$HOME/biblio.bib inotifywait -m $DIR_TMP -e create -e moved_to | while read path action file; do echo $path$file echo $path$file >> $BIB cat $path$file >> $BIB …
hyogapag
  • 125
  • 6