Cutting the first 8 chars of a 2nd column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cutting the first 8 chars of a 2nd column
# 1  
Old 03-26-2008
Cutting the first 8 chars of a 2nd column

I am having a stupid moment :-)

I have a tab-delimited file with 2 columns. I want to keep the first column as it is, but I only want the first 8 characters of the 2nd column.

Code:
Example of input file data:
---------------------------------
CATERPILLARS	CW001651K.dwg
CATERPILLARS	CW001683W.dwg
DUNG BEETLES	DU001751.dwg
DUNG BEETLES	DU001759.dwg
DUNG BEETLES	DU001765_1_2_7455.bak
DUNG FLIES LG	DF001765.dwg
DUNG FLIES XL	DX000211Q.dwg
SLUGS GREEN	SG000282-1110.dwg

Desired Output:
---------------------------------
CATERPILLARS	CW001651
CATERPILLARS	CW001683
DUNG BEETLES	DU001751
DUNG BEETLES	DU001759
DUNG BEETLES	DU001765
DUNG FLIES LG	DF001765
DUNG FLIES XL	DX000211
SLUGS GREEN	SG000282

Thanks very much in advance.
...Cassj
# 2  
Old 03-26-2008
The stream has a tough time distinguishing whitespace from tab chars, so you might want to swap them into visible chars going in, and return them to normal coming out:

Code:
cat my_file |sed 's/\t/\|/g' |while read line
do
col1=$(echo $line |cut -d"|" -f1 )
col2=$(echo $line |cut -d"|" -f2 |cut -b 1-8 )
print "$col1|$col2"
done |sed 's/|/\t/g'

This seds the tab chars into an alternate delimiter and then cuts the fields according to your needs. The entire thing is then reversed on its way out, so that it can be redirected to a second file, etc.

HTH
# 3  
Old 03-26-2008
Except many versions of sed don't understand \t, and that's a pretty wacky thing to do anyway. If you really have trouble coping with tabs then put it in a variable and use that.

Code:
tab='	'  # make sure you get it right this one time (^:
sed -e "/\\(${tab}........\\).*/\\1/"

That's the whole program.

If you want to play around with a while loop then why not let read take care of the splitting. With IFS= you can tell it that the fields are tab separated.

Code:
while IFS='	' read first second  # that's a tab there between the quotes again
do
    echo "$first" | tr '\012' '\011'  # convert first newline to tab
    echo "$second" | cut -c1-8
done <my_file

If you have trouble typing a literal tab at the command line, try ctrl-v tab.

Last edited by era; 03-29-2008 at 07:51 PM.. Reason: Correct octal code for tab, duh
# 4  
Old 03-26-2008
I try my wacky best..thanks. Smilie

Either way works..and this is the UNIX for Dummies forum. Be prepared for some seemingly simplistic approaches here.
# 5  
Old 03-26-2008
Sure, no offense, it just seemed kind of tangential to what the OP was asking for. Hope I managed to entertain more than disturb.
# 6  
Old 03-26-2008
Another approach:

Code:
awk '{printf("%s\t%s\n",$1,substr($2,1,8))}' file

Regards
# 7  
Old 03-26-2008
bah...! I'm plenty disturbed to begin with..but now I'm better educated on this topic.

ksh93 on Windows and ksh88 on Solaris tend to confuse the tab char for spaces..hence my original loop. Besides, I like to be explicit in my code..since it tends to be helpful in deciphering my own mess after the deployment.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

Cutting a column and pasting its number

Hello Gurus, This is my first ever post here. I tried looking for similar material but came up empty handed. Apologies if this is too verbose or if I'm not using the correct formatting. I have files containing a fixed number of elements per line; separator is a single space. Each line has the... (4 Replies)
Discussion started by: stdroid
4 Replies

3. UNIX for Dummies Questions & Answers

Cutting only the 1st column data

Hi All, I having a data file which consists of 20cr records in it. The 1st column is year field which consist of year in format 200809 and fields are seperated with ^. How do i trim it to 2008 in file and save it in a quick time as there are many records so that i can use the file for loading... (3 Replies)
Discussion started by: abhi_123
3 Replies

4. Shell Programming and Scripting

Transpose from 2nd column till the last column

Hi I have 5 columns like this a b c d e f g h i j k l m n o From 2nd column till the 5th column of every record, I would like to transpose them as rows, so my output file contains only one row a b c d e f g h i j (9 Replies)
Discussion started by: jacobs.smith
9 Replies

5. Shell Programming and Scripting

Calculate 2nd Column Based on 1st Column

Dear All, I have input file like this. input.txt CE2_12-15 3950.00 589221.0 9849709.0 768.0 CE2_12_2012 CE2_12-15 3949.00 589199.0 9849721.0 768.0 CE2_12_2012 CE2_12-15 3948.00 589178.0 9849734.0 768.0 CE2_12_2012 CE2_12-52 1157.00 ... (3 Replies)
Discussion started by: attila
3 Replies

6. UNIX for Dummies Questions & Answers

[Q] cutting from one column and pasting to another

I have a file laid out in columns with the first two lines line being: 219 432 4567 219 432 4587 I need to create a single line command to cut the characters in the 5th column and paste them back to the first column in the same file. (Hint:Two good solutions exist, one in which you use a... (9 Replies)
Discussion started by: mcampos7
9 Replies

7. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

8. Shell Programming and Scripting

How to cut first line only from a text near a specific column without cutting a word

First I have to say thank you to this community and this forum. You helped me very much builing several useful scripts. Now, I can't get a solution the following problem, I'm stuck somehow. Maybe someone has an idea. In short, I dump a site via lynx and pipe the output in a file. I need to... (7 Replies)
Discussion started by: lowmaster
7 Replies

9. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

10. Shell Programming and Scripting

Need help recurrently cutting single column file

Hi there ! I have file with single column. I want to cut that column at fixed number of rows each time and paste in another file, in a way that in new file, the each cutting appear as separate columns. I mean cutting file with one column of 10000 rows, with 100 rows each time, and in new file... (3 Replies)
Discussion started by: jacks
3 Replies
Login or Register to Ask a Question