The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-23-2001
sanjay92
Guest
 

Posts: n/a
Question cut

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
The first column of the second row is blank (NULL).

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  
Old 10-23-2001
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
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  
Old 10-24-2001
Registered User
 

Join Date: Oct 2001
Location: Phoenix, AZ
Posts: 15
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  
Old 10-24-2001
sanjay92
Guest
 

Posts: n/a
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
the output is B and E, what I want is B and D.
You are right that cut -c will work perfect , If positions are known.
  #5  
Old 10-24-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Re: cut

Quote:
Originally posted by sanjay92
How to use cut when your delimeter is either space (could be one or more) or tab.
Change the delimiter to be a single tab. Then use cut. Below, where I type TAB, hit your TAB key.

sed 's/[ TAB][ TAB]*/TAB/g' | cut -f1-2
  #6  
Old 10-24-2001
TioTony's Avatar
Bit Pusher
 

Join Date: Oct 2001
Location: Southern California
Posts: 332
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  
Old 10-24-2001
sanjay92
Guest
 

Posts: n/a
Thanks guys.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0