I'm working with a text file that is composed of several hundred comma deliniated strings that I need to parse out and print back to a file. I'm new to AWK and need a little help on how to get started. I'm familiar with C.
Thanks
and so on. The abc's are just representaions, in the file they are all different values. Each string ends with a comma and a return. I need to seperate the fields into something like this,
And so on until the end of the file. I do not need any of the other information within the line of text. I need just the first field, the 5,6 fields, the 7,8 fields, the 9,10 fields, the 11,12 fields, and the 5,6 fields repeated. I also do not need any of the commas at the end of the fields. Here is what happened when I ran what you gave me;
I could not understand what "fields 5,6 repated" meant ...
so I've skipped that logic.
BEGIN {
FS=OFS=",";
SUBSEP=",";
fieldsIN="0,5,6,7,8,9,10";
num=split(fieldsIN, fieldsARR, ","
# printf("%d members in array\n", num);
# for ( i in fieldsARR)
# printf(" %s -> %s\n", i, fieldsARR);
}
{
for (i=1; i <= NF; i++)
if ( i == 1)
printf("%s\n", $i);
else if ( i in fieldsARR) {
if ( fieldsARR % 2 == 0)
printf("%s\n", $fieldsARR);
else
printf("%s%s", $fieldsARR, OFS);
}
}
BEGIN {
FS=OFS=",";
SUBSEP=",";
fieldsIN="0,5,6,7,8,9,10";
num=split(fieldsIN, fieldsARR, ","
# printf("%d members in array\n", num);
# for ( i in fieldsARR)
# printf(" %s -> %s\n", i, fieldsARR);
}
{
for (i=1; i <= NF; i++)
if ( i == 1)
printf("%s\n", $i);
else if ( i in fieldsARR) {
if ( fieldsARR % 2 == 0)
printf("%s\n", $fieldsARR);
else
printf("%s%s", $fieldsARR, OFS);
}
}
sure thing, but I thought the OP wanted a more
generic solution. You could modify the script to pass the "fieldsIN" array as parameter to the script with the fileds
you'd want to extract.
That way you wouldn't have to modify AWK for every possible
field extraction need.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.