LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   using awk to parse csv and exclude field range (https://www.linuxquestions.org/questions/linux-software-2/using-awk-to-parse-csv-and-exclude-field-range-902626/)

rylincoln 09-12-2011 12:44 PM

using awk to parse csv and exclude field range
 
I have a large text file that is comma delimited.
it has 260 fields

it is formatted very basically

field1,field2,field3,...field260

There are no double quotes around any text or anything oddball.

I want to print to a new file excluding field6-field239
This is what I've tried but it's not excluding the fields

http://pastebin.com/k5BNwnNS

anybody offer some help?

Thanks.

rylincoln 09-12-2011 01:15 PM

Nevermind...

gawk -F"," -v f=6 -v t=239 '{ for (i=1; i<=260;i++) if( i>=f && i<=t) continue; else printf("%s%s", $i,(i!=0) ? OFS : ORS) }' tx000452010.txt


solved.

facepalm

chrism01 09-12-2011 07:21 PM

Just as an alternative
Code:

cut -d',' -f1-5,240-260 filename
would do the same
:)

grail 09-12-2011 11:26 PM

Alternate awk:
Code:

awk 'BEGIN{ORS=RS=","}NR < 6 || NR > 239' file


All times are GMT -5. The time now is 05:22 AM.