Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?
# 1  
Old 09-28-2008
Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody,

I am having much trouble figuring this out, as I am not really a programmer..Smilie

Datafile.txt
Column0 Column1 Column2
ABC DEF xxxGHI

I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from column2 of the file, and save the file. It is Tab-delimited...

Any help at all would be greatly appreciated!!! I have been reading tutorials for hours and I just can't get it!

Thanks in advance,
Rick
# 2  
Old 09-28-2008
Oh yea, and I have to make sure I don't run this command on the Column Headers....
# 3  
Old 09-28-2008
With awk:

Code:
awk 'NR>1{print substr($3,1,3)}' file > newfile

Regards

Last edited by Franklin52; 09-28-2008 at 05:33 PM.. Reason: change column number, the OP means $3 with Column2
# 4  
Old 09-28-2008
Franklin52,

Thank you for this - it is resolving with the correct conent. However, I am just ending up with a file the extracted content:

AAH
AAJ
AAI
AAL
AAM ...and so on.

I need to re-save the original document with those letters removed from the beginning of Column2 data. Should I do a SED find and replace?

I apologize - I am very new to this...Thanks in advance!
# 5  
Old 09-28-2008
Try this:

Code:
awk 'NR==1{print;next}{$3=substr($3,4)}1' OFS="\t" file > newfile
mv newfile > file

Examine the output of newfile before you replace the original file.

Regards
# 6  
Old 09-28-2008
Hey Franklin,

A strange thing is happening with the output, and I believe it is due to the fact that the columns contain spaces and lots of text.

The script took the spaces in Column0 and made them into new columns, then trimmed the new Column2.

My original data reads like this:

Column 0 is the product name:
"Shure KSM109 Condenser Microphone"

Column 1 is description, which is a paragraph of text:
"The Shure KSM109 offers incredible performance for its price. It combines an extended...."

Column 2 is the Manufacturer code, which needs to be trimmed:
SHUKSM109SL <---(I need to chop SHU)



The output data is looking like this:

Column 0: "Shure"
Column 1: "KSM100"
Column 2: "denser"

So basically it looks like it is treating spaces as tabs. I really appreciate it this help Franklin - thanks so much!

- Rick
# 7  
Old 09-29-2008
I disn't assume that you have spaces in the fields. This should work with spaces in fields:

Code:
awk 'BEGIN{FS=OFS="\t"}NR==1{print;next}{$3=substr($3,4)}1' file > newfile

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

4. Shell Programming and Scripting

Delete an entire column from a tab delimited file

Hi, Can anyone please tell me about how we can delete an entire column from a tab delimited file? Mu input_file.txt looks like this: And I want the output as: I used the below code nawk -v d="1" 'BEGIN{FS=OFS="\t"}{$d=""}{print}' input_file.txtBut in the output, the first column is... (5 Replies)
Discussion started by: sampoorna
5 Replies

5. Shell Programming and Scripting

Delete string between 3rd tab and first pattern with SED

Hello, I have this sentence :Pattern1 Pattern2 Pattern3 Pattern4-which-contains-HELLO-string-and-other-stuff-and-second-HELLO-and-third-HELLO I want to delete everything between the 3rd tab (\t) and the FIRST pattern "HELLO" of the line. Result expected is : Pattern1 ... (7 Replies)
Discussion started by: theclem35
7 Replies

6. UNIX for Dummies Questions & Answers

add (append) a column in a tab delimited file

I have a file having the following entries: test1 test2 test3 11 22 33 22 44 66 99 99 44 --- I want to add a column so that the above file becomes: test1 test2 test3 notest 11 22 33 * 22 44 66 * 99 99 44 * --- Thanks (6 Replies)
Discussion started by: mary271
6 Replies

7. Shell Programming and Scripting

Extract second column tab delimited file

I have a file which looks like this: 73450 articles and news developmental psychology 2006-03-30 16:22:40 1 http://www.usnews.com 73450 articles and news developmental psychology 2006-03-30 16:22:40 2 http://www.apa.org 73450 articles and news developmental psychology 2006-03-30... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. UNIX for Dummies Questions & Answers

Using awk to log transform a column in a tab-delimited text file?

How do I use awk to log transform the fifth column of a tab-delimited text file? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

Using sed on 1st column of tab delimited file

Hi all, I'm new to Unix and work primarily in bioinformatics. I am in need of a script which will allow me to replace "1" with "chr1" in only the first column of a file which looks like such: 1 10327 rs112750067 T C . PASS ASP;RSPOS=10327;... (4 Replies)
Discussion started by: Hkins552
4 Replies

10. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies
Login or Register to Ask a Question