Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using AWK to Trim 3rd Column of Tab-delimited text file - need help

Status
Not open for further replies.

rickdini

Technical User
Sep 28, 2008
1
US
Hey guys,

I am running a cronjob that downloads a datafeed text file and I need to trim the first 3 characters from the 3rd column data (excluding the header).

This is what I have so far:

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

Column Zero is Product Name, which has multiple words and Spaces. Column One is a long text description, and column two is the SKU, which I need to trim (i.e. xxxABCDEFG > ABCDEFG).

ISSUE:
The above code breaks my datafile and makes new columns for every SPACE. It seems like the \t is not working correctly. Instead, it's stripping all of the tab delimitation and spacing every word.

Any help with this would be greatly appreciated. Thanks in advance!!!

- Rick
 
Per default, awk breaks up a line into columns on any whitespace string. You can instruct awk to use a specific FieldSeparator with -F

[tt]awk -F "\t" 'awk program' /path/to/file[/tt]

Now if you also want to change the output field separator, use a BEGIN section:

[tt]awk 'BEGIN{FS="\t"; OFS="\t"} {main program text}' /path/to/file[/tt]


HTH,

p5wizard
 
Hi

Or a shorter way I learned in this forum :
Code:
awk -F'\t' -vOFS='\t' '{main program text}' /path/to/file
Certainly works with [tt]gawk[/tt] and [tt]mawk[/tt], not sure if with your [tt]awk[/tt] implementation too.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top