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!

print lines 1

Status
Not open for further replies.

inetm

Technical User
May 11, 2004
3
US
I am a beginner with gawk. I am having a rough time with this can you help me. I can get certain fields but always end up with the header and all the other data and empty lines.
Sample
11
1234
---------------------------------------------
some.| some |some | some. |some
------------------------------------------------
1| thing1|thing2| thing3|thing4 |
2| thing1|thing2| thing3|thing4 |
3| thing1|thing2| thing3|thing4 |
4| thing1|thing2| thing3|thing4 |

344
5632
---------------------------------------------
OTher.| some |some | some. |some
------------------------------------------------
1| Filed1|Filed2| Filed3|Filed4 |
2| Filed1|Filed2| Filed3|Filed4 |
3| Filed1|Filed2| Filed3|Filed4 |
4| Filed1|Filed2| Filed3|Filed4 |

Would like

thing1 thing3
Filed1 Filed3
but with the option to add other fields etc.
Many Thanks
 
sure, no problem.

gawk -v things='1:3' things.awk

here's things.awk:
Code:
BEGIN {
  n=split(things, thingsA,":");
  for(i=1; i <=n; i++)
     printf("thing%d%s", thingsA[i], (i !=n ) ? OFS : "\n");
    for(j=1; j <=n; j++)
     printf("Field%d%s", thingsA[j], (j !=n ) ? OFS : "\n");
}

Now....... what do you really want to do?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Not very elegant but may do what you want. The hard part
is being able to specify additional fields to print dynamically if that is what you want.

Code:
awk -v more=0 '{
                    if ($0 ~ /[0-9]\|/) {
                     split($0,arr,"|")
                     if (more > 0) {
                                 print arr[2],arr[4],arr[more]
                   } else {
                   print arr[2],arr[4]
                   }
}'


OP:
more=0
Code:
  thing1     thing3
       thing1     thing3
       thing1     thing3
       thing1     thing3
       Filed1     Filed3
       Filed1     Filed3
       Filed1     Filed3
       Filed1     Filed3
more=1
Code:
   thing1     thing3    1
       thing1     thing3    2
       thing1     thing3    3
       thing1     thing3    4
       Filed1     Filed3    1
       Filed1     Filed3    2
       Filed1     Filed3    3
       Filed1     Filed3    4
 
Code:
BEGIN { fields="2 4" ; nf = split(fields,fa)
        FS = " *[|] *" }
6==NF { for (i=1; i<=nf; i++)
          printf "%s%s", $fa[i], (i==nf) ? "\n" : " " }
[tt]
thing1 thing3
thing1 thing3
thing1 thing3
thing1 thing3
Filed1 Filed3
Filed1 Filed3
Filed1 Filed3
Filed1 Filed3
[/tt]
 
Thanks for all of the posts / help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top