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!

Editing a comma deliniated string

Status
Not open for further replies.

krpst19

Programmer
Jun 22, 2001
10
US
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
 
The file I'm working with looks like this,

a,b,c,d,e,f,g,h,i,
a,b,c,d,e,f,g,h,i,

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,

a
c,d
e,f
g,h

a
c,d
e,f
g,h

This all comes from one input.txt file
Thanks
 
do you mean 2 fields per line?


BEGIN {
FS=OFS=",";
}

{
for (i=1; i <= NF; i++)
if ( i % 2 == 0)
printf(&quot;%s\n&quot;, $i);
else
printf(&quot;%s%s&quot;, $i, OFS);
}

 
I'll try that,
thanks for your expediant help. I'll let you know how it turns out.
 
Here is a portion of the exact text file that I'm working with:


AST_L1A.002:2002141577,Aug-17-00-11:16:29,91.3174,71.9374,14.8706,-6.8062,14.7895,-6.2338,14.2312,-6.3565,14.3122,-6.9275,28,34,37,40,35,1226,01.02,01.02,
AST_L1A.002:2002141581,Aug-17-00-11:16:38,89.8660,71.8814,14.3356,-6.9224,14.2546,-6.3514,13.6964,-6.4737,13.7773,-7.0434,98,93,82,69,86,1226,01.02,01.02,
AST_L1A.002:2002141584,Aug-17-00-11:16:47,88.4279,71.8144,13.8008,-7.0383,13.7199,-6.4686,13.1616,-6.5906,13.2424,-7.1591,94,99,98,99,97,1226,01.02,01.02,
AST_L1A.002:2002141585,Aug-17-00-11:16:55,86.9993,71.7368,13.2658,-7.154,13.185,-6.5855,12.6267,-6.7073,12.7073,-7.2745,95,93,97,96,95,1226,01.02,01.02,
AST_L1A.002:2002141589,Aug-17-00-11:17:04,85.5853,71.6486,12.7309,-7.2695,12.6502,-6.7022,12.0918,-6.8236,12.1724,-7.3897,100,100,99,99,99,1226,01.02,01.02,
AST_L1A.002:2002141590,Aug-17-00-11:17:13,84.1839,71.5505,12.1959,-7.3847,12.1154,-6.8185,11.5569,-6.9397,11.6373,-7.5047,100,100,100,100,100,1226,01.02,01.02,


What I need exactly is the following;

AST_L1A.002:2002141584
14.8706,-6.8062
14.7895,-6.2338
14.2312,-6.3565
14.3122,-6.9275
14.8706,-6.8062

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;

AST_L1A.002:2002141577,Aug-17-00-11:16:29
91.3174,71.9374
14.8706,-6.8062
14.7895,-6.2338
14.2312,-6.3565
14.3122,-6.9275
28,34
37,40
35,1226
01.02,01.02
,AST_L1A.002:2002141581,Aug-17-00-11:16:38
89.8660,71.8814
14.3356,-6.9224
14.2546,-6.3514
13.6964,-6.4737
13.7773,-7.0434
98,93
82,69
86,1226
01.02,01.02


Thank you so much for your help,
 
try this one.

I could not understand what &quot;fields 5,6 repated&quot; meant ...
so I've skipped that logic.


BEGIN {
FS=OFS=&quot;,&quot;;
SUBSEP=&quot;,&quot;;
fieldsIN=&quot;0,5,6,7,8,9,10&quot;;
num=split(fieldsIN, fieldsARR, &quot;,&quot;);
# printf(&quot;%d members in array\n&quot;, num);
# for ( i in fieldsARR)
# printf(&quot; %s -> %s\n&quot;, i, fieldsARR);

}

{

for (i=1; i <= NF; i++)
if ( i == 1)
printf(&quot;%s\n&quot;, $i);
else if ( i in fieldsARR) {
if ( fieldsARR % 2 == 0)
printf(&quot;%s\n&quot;, $fieldsARR);
else
printf(&quot;%s%s&quot;, $fieldsARR, OFS);
}
}

 
ooops - copy/paste really bites - sorry

BEGIN {
FS=OFS=&quot;,&quot;;
SUBSEP=&quot;,&quot;;
fieldsIN=&quot;0,5,6,7,8,9,10&quot;;
num=split(fieldsIN, fieldsARR, &quot;,&quot;);
# printf(&quot;%d members in array\n&quot;, num);
# for ( i in fieldsARR)
# printf(&quot; %s -> %s\n&quot;, i, fieldsARR);

}

{

for (i=1; i <= NF; i++)
if ( i == 1)
printf(&quot;%s\n&quot;, $i);
else if ( i in fieldsARR) {
if ( fieldsARR % 2 == 0)
printf(&quot;%s\n&quot;, $fieldsARR);
else
printf(&quot;%s%s&quot;, $fieldsARR, OFS);
}
}
 
If I understand correctly, you could just do this;
Code:
awk -F&quot;,&quot; '{printf(&quot;%s\n%s,%s\n%s,%s\n%s,%s\n%s,%s\n%s,%s\n&quot;,$1,$5,$6,$7,$8,$9,$10,$11,$12,$5,$6)}'
Greg.
 
sure thing, but I thought the OP wanted a more
generic solution. You could modify the script to pass the &quot;fieldsIN&quot; 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.

Oh well.... whatever make the OP happy...

vlad
 
Thanks for all your help. I tried it Vlad's way and it worked. I greatly appreciate
your time.

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top