You may often need to slit a file in such way that output should be only some or few columns.
Unix/ Linux comes with powerful sword for this and its called cut ( the name is simple to guess )
Lets consider following file for this example:
now if you want to see first character of each row, in other words if you want to "slit vertically in such a way that you only see red highlighted part of the file, you should try to play with cut
and that is how you do it
you can even specify range off characters, see below two examples
and off course you can join two different range of characters as well, hope below example will explain you this
Now if you don't know the end of your files that means if you don't know, how many numbers of characters each row is having and still you want to see the all characters till end, starting with certain character numbers
of each line use "-" as below ( starting from 9th character in this case )
cut command is quite versatile it even works with delimiter, all you have to do specify delimiter ( see red highlighted part ) and name the fields you want want to based on delimiter you specified
Few more examples :
you can specify more then one field
and range of fields too
you must have observed the difference between cut -d "," -f1,3 Example-1.txt and cut -d "," -f1-3 Example-1.txt , in first case it only prints specified columns (1st and 3rd) in second case it prints range of column ( 1st to 3rd ).
I am concluding this post here , cut is a great text formatting tool when used with some other unix commands, I will discuss them in my future posts
Thanks for reading , have a nice day :)
if you want learn to Basic Unix Commands in 1 Hour, here is the link
Basic Unix Commands in 1 Hour
if you want learn Unix/Linux Commands in detail, here is the link
Learn Unix/Linux Commands in detail
Also keep visiting my blog to learn more
unixtechworld.blogspot.com
Unix/ Linux comes with powerful sword for this and its called cut ( the name is simple to guess )
Lets consider following file for this example:
now if you want to see first character of each row, in other words if you want to "slit vertically in such a way that you only see red highlighted part of the file, you should try to play with cut
and that is how you do it
you can even specify range off characters, see below two examples
and off course you can join two different range of characters as well, hope below example will explain you this
Now if you don't know the end of your files that means if you don't know, how many numbers of characters each row is having and still you want to see the all characters till end, starting with certain character numbers
of each line use "-" as below ( starting from 9th character in this case )
cut command is quite versatile it even works with delimiter, all you have to do specify delimiter ( see red highlighted part ) and name the fields you want want to based on delimiter you specified
Few more examples :
you can specify more then one field
and range of fields too
you must have observed the difference between cut -d "," -f1,3 Example-1.txt and cut -d "," -f1-3 Example-1.txt , in first case it only prints specified columns (1st and 3rd) in second case it prints range of column ( 1st to 3rd ).
I am concluding this post here , cut is a great text formatting tool when used with some other unix commands, I will discuss them in my future posts
Thanks for reading , have a nice day :)
if you want learn to Basic Unix Commands in 1 Hour, here is the link
Basic Unix Commands in 1 Hour
if you want learn Unix/Linux Commands in detail, here is the link
Learn Unix/Linux Commands in detail
Also keep visiting my blog to learn more
unixtechworld.blogspot.com
No comments:
Post a Comment