![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to use cut when your delimeter is either space (could be one or more) or tab.
example :- See the data below. Code:
A B C
E F
say, I want to cut the second field. The fields are seprated by either one or more blank space or one or more tab. Do, I have use tr command first on the file and then pass the result to the cut command or there is any easy way in cut command itself. Thanks Sanjay Last edited by sanjay92; 10-23-2001 at 09:24 PM. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Personally, I would try to use awk instead of cut, especially if the data in the columns ins't lined up. If the data is lined up, why not just use cut -c?
|
|
#3
|
|||
|
|||
|
I agree, awk would be the way to go:
awk '{print $2 }' file1 > file2 this will pull out your column B from file1 and put it into file2 (for other columns, change the $2 to what ever column # you need. (you can even pull multiple columms If this won't work, and you still need to use cut, you can replace the spaces with a comma, or what ever delimiter you need. Search & Replace in VI in vi type :%s/ /,/g This will replace all tabs with a comma to replace multiple spaces, or multiple tabs, just put the number of spaces or tabs in the first part of the search & replace RECON |
|
#4
|
|||
|
|||
|
Thanks Guys,
I guess awk command will not work perfectly here. cat y | awk '{ print $2 }' where y is Code:
A B C
D E
You are right that cut -c will work perfect , If positions are known. |
|
#5
|
||||
|
||||
|
Re: cut
Quote:
sed 's/[ TAB][ TAB]*/TAB/g' | cut -f1-2 |
|
#6
|
||||
|
||||
|
You can still use awk, you just need the -F option. awk -F\t '{print $2}' will give you the second column if it is truely tab delimited.
|
|
#7
|
|||
|
|||
|
Thanks guys.
|
|||
| Google The UNIX and Linux Forums |