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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with AWK!

Status
Not open for further replies.

abdu22

Programmer
Sep 13, 2005
14
FR
Hi All,

I have a file with 1215450 lines long, here is few lines:

1.06189E-08 0.6320 0.00000E+00 0.0000 0.00000E+00 0.0000 2.66971E-09 1.0000
0.00000E+00 0.0000 0.00000E+00 0.0000 0.00000E+00 0.0000 0.00000E+00 0.0000
1.27355E-10 1.0000 7.50482E-10 1.0000 0.00000E+00 0.0000 0.00000E+00 0.0000
0.00000E+00 0.0000 0.00000E+00 0.0000 0.00000E+00 0.0000 0.00000E+00 0.0000

I'd like to print $1, $3, $5, $7 from each line and put the results in lines with 146 fields ==> 1215450 lines * 4 fields ($1, $3, $5, $7) = 4861800 fields

4861800 fields / 146 fields = 33300 lines


i.e. the final file will have 33300 lines with 146 fields for each line... I can simply print ($1, $3, $5, $7) but how can I put them in 146-field lines?..

Thanks
 
According to my knowledge, I found this method in two steps :

1- awk '{if (NF==8) printf ($1"\n"$3"\n"$5"\n"$7"\n")}' input > out

2- awk 'BEGIN {j=0} {if (NF>0) printf ($1" "); j++;} {if (j==146) {printf ("\n"); j=0;}}' output > Results


 
note: untested, but you can take it from here... (144 fields per record would have been easier ;-))

Code:
cat input|awk 'BEGIN{
 count=0
}
{
 if(count>0) {
  printf(" ");
 }
 printf("%s %s", $1, $3);
 count+=2;
 if (count==146){
  printf("\n");
  count=0;
 }
 else {
  printf(" ");
 }
 printf("%s %s", $5, $7);
 count+=2;
 if (count==146){
  printf("\n");
  count=0;
 }
}
END{
 printf "\n"
}'

HTH,

p5wizard
 
p5wizard said:
cat input|awk 'BEGIN{
cat isn't needed. Instead of
[tt]cat data | foobar[/tt]
use
[tt]foobar <data[/tt]

abdu22, try this:
Code:
BEGIN { fields_per_line=146; split("1 3 5 7",fields)
  ORS="" }
NF {
  for (i=1; i in fields; i++)
  { print $i
    count++
    if ( fields_per_line == count )
    { print "\n"
      count = 0
    }
    else
      print OFS
  }
}
 
awk and paste...
Code:
awk -v OFS='\n' '{print $1,$3,$5,$7}' file1| paste -s -d '[i]<145 spaces>[/i]\n'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top