How To Split or Cut A File Vertically(Column wise)

April 6th, 2009

In Unix environment it’s often required to print specific columns from a file. If the rows of file are having some proper delimiters then other Unix command like awk come into picture and quite handy. But if say you want to print out the 5th character and then 7th to 16th character, then other commands fail and in such situations the cut command comes to rescue. Here is short primer of cut command.

For our example I have taken a file named as list. Its output is pasted below:

[ganesh@host ~]$ cat list

-rwxr-xr-x|1|root|root|1904|Nov|10|2007|crond

-rwxr-xr-x|1|root|root|1505|Jan|6|2007|dc_client

-rwxr-xr-x|1|root|root|1347|Jan|6|2007|dc_server

Let’s say you want the 4th column from the above output.

Use the below command:

cut -c4 list

Here c signifies the number of column.

The output is:

[ganesh@host ~]$ cut -c4 list

x

Now let’s say you want 3rd to 10th column. You can specify mth to nth fields as m-n as you’ll see in examples. Use the below command:

cut -c3-10 list

[ganesh@host ~]$ cut -c3-10 list

wxr-xr-x

wxr-xr-x

wxr-xr-x

Now let’s say you want 4th to 16th column and everything after 20th column

Use the below command:

cut -c4-10,16- list

[ganesh@host ~]$ cut -c4-10,16- list

xr-xr-xot|root|1904|Nov|10|2007|crond

xr-xr-xot|root|1505|Jan|6|2007|dc_client

xr-xr-xot|root|1347|Jan|6|2007|dc_server

Remember there is no space provided between multiple cuts taken from a file. You see -xot in the output. xr-xr-x is one output and ot|root|1904|Nov|10|2007|crond is another output.

Now let’s say the file is delimited by a separator e.g ‘|’. Here cut gives you a special option ‘-d’. It’s used to specifiy the delimiter and then the filed on it.

The following example makes it a little bit clear.

Suppose you want to get the 1st to 3rd field, 5th to 6th field and then 8th onwards all fields, based on | as delimiter.

Use the command:

cut -d ‘|’ -f 1-3,5-6,8- list

[ganesh@host ~]$ cut -d ‘|’ -f 1-3,5-6,8- list

-rwxr-xr-x|1|root|1904|Nov|2007|crond

-rwxr-xr-x|1|root|1505|Jan|2007|dc_client

-rwxr-xr-x|1|root|1347|Jan|2007|dc_server

In some future post I’ll write about pasting the outputs.

Tags: , , , , , ,
Posted in Tips and Tricks | 2 Comments »

Comments

2 Responses to “How To Split or Cut A File Vertically(Column wise)”

  1. Kishor Ahuja Says:

    Hi Ganesh,
    Let me know the ways to ZIP the complete directory structure on Unix system. If there are multiple ways then which one is best and why? I have to move applications from one server to another, for that I need this information.

    Any help will be appriciated.

  2. Gowri Says:

    Ganesh,
    it would more helpfull if you share me the video ‘s with respect to LPAr creation and allocating the virtual resources to the created lpar.

Leave a Reply

 Comment Form 

 



More articles from the category: Tips and Tricks


What is The Difference Between exec and xargs

There are very small things in linux based systems which often consfuse the users. Here in this article I’m going to discuss the specific use of find command and difference between exec and xargs.

Understanding The diff Command In Unix

It’s not an uncommon requirement for Unix system administrators to know the difference between two files. The diff command in Unix serves the purpose. Here I am going to discuss the diff command. It’s quite common but little understood command. I hope after reading this article, the Unix visitors will be able to understand the usage properly and benefit from it. Here you go…

How To Find Common Lines Between Two Text Files In Unix

Those working in Unix environment must be aware of the requirement to find common
lines between two Unix files. In this article I’m gonna introduce you to solve this problem.

How To Join Two Files Vertically

In Unix, sometimes we come across situations where we need to join two files’ output side by side vertically. This is also referred to as vertical joining of files. In such situations, paste command comes handy. With the help of this command you not only can vertically join the files, but insert some delimiter as well….

How To Recover A Superblock

If fsck or mount commands give errors then pretty good chances are there that the superblock is corrupt. The dd command comes to rescue. We know that the superblock resides in 31st block also. So, to recover that we use the following command: # dd count=1 bs=4k skip=31 seek=1 if=/dev/my_lv  of=/dev/my_lv count: How many blocks […]

Ways To Zip The Directory Structure of Unix

Often its the requirement of system administrators to move the directories in between the servers. Also the confusion arises which utility to use for the best results. Normally zip, tar, cpio utilities are used the most. I’m writing a short introduction of zip and tar.

How To Split or Cut A File Vertically(Column wise)

In Unix environment it’s often required to print specific columns from a file. If the rows of file are having some proper delimiters then other Unix command like awk come into picture and quite handy. But if say you want to print out the 5th character and then 7th to 16th character, then other commands fail and in such situations the cut command comes to rescue. Here is short primer of cut command.