Linux Tips and Tricks

Linux Tips and Tricks

The Cat Command

November4

 cat

 

Normally we use the cat command to display the small files. And sometimes we use to create a file.

we can use the cat command for the following purpose.

1) Display the line number.

2) Display the end of the line with $

3) squeeze Multiple blanks to one blank line.

4) view tab seperated lines

Now we see the examples.

1) Display the line number.

$ cat test_script.sh
#!/bin/sh
yesterday=`date -d "yesterday" +%Y%m%d`
echo $yesterday

$ cat -n test_script.sh
     1    #!/bin/sh
     2    yesterday=`date -d "yesterday" +%Y%m%d`
     3    echo $yesterday
 

Note : You have to use the -n option with the cat command to display the line numbers.

2) Display the end of the line with $

echo "test" >> Test
echo "" >> Test
echo "testing" >> Test

cat -e Test

test$
$
testing$
 

Note: Every line of the end you can see the letter $. Just create a line with some space and see where the $ symbol goes.

3) squeeze Multiple blanks to one blank line.

The following commands shows that in the testing_file we have some empty lines.

$ cat -n testing_file
     1    test
     2   
     3   
     4   
     5   
     6   
     7   
     8    another line
     9   
    10   
    11   
$ cat -e testing_file
test$
$
$
$
$
$
$
another line$
$
$
$
 

Now use the -s option in the cat command,

-s, –squeeze-blank
              suppress repeated empty output lines

$ cat -s testing_file
test

another line

$
 

4) view tab seperated lines

I created a file called tab_test with tab seperated words.

$ cat tab_test
this    is    tab    seperated    line
testing    for    cat    command
 

Now i am gonna include the -t option with the cat command.

       -T, –show-tabs
              display TAB characters as ^I


$ cat -t tab_test
this^Iis^Itab^Iseperated^Iline
testing^Ifor^Icat^Icommand
$

In the about output you can see the tab space is filled with ^I (cap symbol followd by capital i )

$ echo "this is not tab seperated line" >> tab_test

$ cat -t tab_test
this^Iis^Itab^Iseperated^Iline
testing^Ifor^Icat^Icommand
this is not tab seperated line

 

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories