Saturday, 28 January 2012


cat  can be used to open  one or a group of  files ,  append  files into one file. one  of the special functionality is  to read an entire file  line by line along with the read command. The examples will show us how.
Example 1)
/home/b3456/$ cat  branches1.txt                      (Note: here /home/b3456/$ is the prompt
35467       vdn          /home/vd2
46788       ghi        /home/gh1/gh
89078       bjk       /home/vd2

/home/b3456/$ cat  branches2.txt
56890      lod          /home/lod/inter
33456       bhj       /home/bhind
45790       krk       /myhome/krk

Combining these two files into a single file can be done by
/home/b3456/$ cat  branches1.txt  cat  branches2.txt

35467       vdn          /home/vd2
46788       ghi        /home/gh1/gh
89078       bjk       /home/vd2
56890       lod          /home/lod/inter
33456       bhj       /home/bhind
45790       krk       /myhome/krk

          Or by
/home/b3456/$  cat  branches[1-2].txt

Suppose you have many such files named branches1.txt branches2.txt branches3.txt… branches<x>.txt  where  <x>  is any  single numeric or alphabetic character, you can use.
cat  branches?.txt to open all  the files.

Example 2) 
You want to write contents you have copied from a file into another file. one way to do it is to open it in a vi editor and then save it.
Other way to do it is by using cat to open a file and paste the contents.

/home/b3456/$ cat  >filetocopy
#the  sentence of these lines
were copied from a different file
and  pasted here.
#the contents here were typed manually

/home/b3456/$more   filetocopy
#the  sentence of these lines
were copied from a different file
and  pasted here.
#the contents here were typed manually


A  ^d  character(ctrl + d) must be typed at the end to mark the end of file.
If you already have  a file with the same name and you want to append the contents copied  into that file,
then use  “>>”  instead of  ‘>’

Example 3)
you can also use cat to read each line of a file into a variable, using read command.
This  cannot be achieved with a ‘for   in ’ loop since it considers words in a line as separate values assigned to the index variable.

The  following  commands  will tell you  how to achieve it using cat and read with a  while loop.
Consider that you want to  read the file  branches1.txt(example 1) ,in such a way that entire string in  each line is stored  into a variable and then  that variable  is used to extract each fields(separated by spaces)

/home/b3456/$ cat   branches1.txt  | while read line  # the variable line stores the contents of a line
num=`echo $line  |  awk  ‘{print $1}’
path=` echo $line  |  awk  ‘{print $3}’
echo  “$path is  the path for $num”

     /home/vd2   is the path for  35467
      /home/gh1/gh   is the path for  46788
     /home/vd2 is the path for  89078

The ‘for in’ loop imposes restrictions on the number of lines of a file which can be  by it. But cat has no limitations. It can read any  file, however large it is until the system resource is exhausted.

Example 4)
line numbers of  a  file can be displayed by using  cat with –n option.
/home/b3456/$ cat  -n   branches1.txt  cat  branches2.txt
    1  35467       vdn          /home/vd2
    2  46788       ghi        /home/gh1/gh
    3  89078       bjk       /home/vd2
    4  56890       lod          /home/lod/inter
    5  33456       bhj       /home/bhind
    6  45790       krk       /myhome/krk

No comments:

Post a Comment