cat

 in
Here's a spiffy little command that can be used to combine files, look at the contents of a file and do limited text editing.
______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Good info

Pharmf521's picture

Hello! dbdegdd interesting dbdegdd site!

cat command for several grib files

Mubashar's picture

I have many grib files (starting from ecmwf19860101.grb to ecmwf20090101.grb). These are basically daily files and the total number of files is nearly 30000 and I want to cat them into one file. When I use the command:

cat ecmwf*>ecmwf.grb
-bash: /bin/cat: Argument list too long

Why its not working on large number of files?
Is there any limit of using "cat" command?

Although I used above command for some of the files and it works.

Please advise me if it is possible to cat large number of grib files?

Thanks in advance.

Regards,

Mubashar

Problem in unix scripting

Lakshmi's picture

Hi , i am facing the problem in unix shell scripting.

#!/bin/sh
if[$# -ne 2]
then
echo"Numbers not supplied"
echo"Usage $0 number1 number2"
exit 1
fi
echo"the sume is `expr $1 + $2`"

not able to execute.

giving the error

./test: line 2: if[0 != 2]: command not found
./test: line 3: syntax error near unexpected token `then'
./test: line 3: `then'

Please help ASAP

Regards,
Lakshmi

White space

Mitch Frazier's picture

You're missing some needed whitespace:

- Between the "if" keyword and the "[" command.
- After the "[" command.
- Before the "]" argument to the "[" command.
- After the echo commands and their arguments.

#!/bin/sh
if [ $# -ne 2 ]
then
echo "Numbers not supplied"
echo "Usage $0 number1 number2"
exit 1
fi
echo "the sume is `expr $1 + $2`"

It's also considered "good" practice, but not mandatory, to indent the commands within an "if" statement:

#!/bin/sh
if [ $# -ne 2 ]
then
    echo "Numbers not supplied"
    echo "Usage $0 number1 number2"
    exit 1
fi
echo "the sume is `expr $1 + $2`"

Mitch Frazier is an Associate Editor for Linux Journal.

1997...?

Anonymous's picture

1997? Is this correct?
The time stamp of the posting says: "October 1st, 1997 by Patrick Hill"

That's only... 12 years ago! :x

No, I just posted this in

Dif's picture

No, I just posted this in September of 2009

Using Cat

Dig's picture

Hi,

I have used the cat command to put the contents of a large number of files into one large file. So I have each files contents in this large file without a blank line between the files. How can I add the filename before the contents of the file and a blank line between the files in my large output file?

Using Cat

Dig's picture

OK, I have all the filenames showing before the contents and with a carriage return after the filename. I just need to know how to add a carriage return after each files contents. Here is the code.

#!/bin/sh

# no error checking
rm -f newfile
for i in *.spc;
do printf "%s\n" $i >> newfile
cat "$i" >> newfile
done

To print specific range of lines using cat

imtiyaz hussain's picture

Hello Patrick,

I am using AIX Unix. How is it possible to select lines to be printed using cat ? If the file size is very big and say it has more than thousand lines and i want to print lines 65 to 70 on standard output.

Regards,
Immi.

To print specific lines using cat and sed commands

Gibies George's picture

Hello Immi.

You can pipe the output of cat command to sed command as follows.

cat filename |sed -n '65,70p'

This will print lines 65 to 70 on standard output.

If you want to print only one line, you can use the following syntax.

cat filename |sed -n '5p'

This will print only line 50 of the file on standard output.

Sorry for the mistake.

Gibies George's picture

Sorry for the mistake.

please read the last line of the my commend corrected as "...line 5 of the file...."

Re: Take Command: cat

Anonymous's picture

Good examples. Something puzzles me though. I am taking a Unix class using Solaris 2.8 on Sun workstations, but I do my practicing on a PC with Linux (RedHat 8). In Unix you can create a new file with:

cat fileName
blah blah
Ctrl-D

But with Linux this produces a "no such file" error, so you must explicitely redirect the output with:

cat > fileName ...

I have tried it with all of the shells installed and get the same. I wonder why the difference?

Re: Take Command: cat

Anonymous's picture

i am very thankful for the detailed information
of the cat command. many of the linux sites
are not having the basic information of cat
command which is must. i pity the negligence
attributed by the linux site maintainers.

any how i am thankful for the detailed and
friendly information.

thanking you

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState