Add a comma delimin to a cut/awk fixed width

September 5, 2010 at 07:51:11
Specs: Windows XP
I have a huge file, fixed width 500 characters wide. I would like to 'cut' roughly 125 of these characters out prior to FTPing. I have a cut command which works (some are ranges, some are single fields, but, it works).

However, I would like to separate each of the parsed fields with a comma in the output file for ease of ORCL import.

Currently:

cut -c12-20,21-29,32......477-491 inputfile.txt>outputfile.txt

It runs, however, I would like to a comma between field20 and 21, 29 and 30 etc.


See More: Add a comma delimin to a cut/awk fixed width

Report •

#1
September 5, 2010 at 10:44:06
cut --output-delimiter="," -c12-20,21-29,32......477-491 inputfile.txt>outputfile.txt

The original poster should always write the last response !!!
Let us know, if the problem is solved !!!


Report •

#2
September 6, 2010 at 08:47:14
The cut command's output-delimiter is a GNU extension which is relatively new. If you aren't using using GNU, a way is to use awk to print out each field adding a comma:

# UNTESTED
cut  -c12-20,21-29,32......477-491 inputfile.txt | awk '
{
for(i=1; i<=NF; i++)
   printf("%s,", $i)
print "\n"
} ' > outputfile.txt


Report •

#3
January 9, 2011 at 05:37:25
I'm sorry I didn't reply sooner, I actually found a different solution and have been working ridiculous hours... by the time I realized I had a response I had forgotten my login/psswd... just getting back to this now.

This issue however is raising its ugly head again and I will be testing these solutions today. I appreciate the responses.

Again, thanks everyone for the responses... I've been re-introducing myself to awk recently, so I will give these a try.


Report •

Related Solutions

#4
January 11, 2011 at 08:30:12
The cut --output-deliminator="," is exactly what I needed, thank you. This solves a huge headache for me.

Report •

Ask Question