Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers cut
# 1  
Old 10-24-2001
Question cut

How to use cut when your delimeter is either space (could be one or more) or tab.

example :-
See the data below.

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.


Last edited by sanjay92; 10-24-2001 at 01:24 AM..
# 2  
Old 10-24-2001
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
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

# 4  
Old 10-24-2001
Thanks Guys,
I guess awk command will not work perfectly here.

cat y | awk '{ print $2 }'

where y is

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
Re: cut

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
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
Thanks guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question