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!

print range of colums.

Status
Not open for further replies.

baer

Technical User
Jul 15, 2001
4
US
I have a simple question. I have a file with 160 columns. If I only wish to print the first 120 column, how do I specify a range? I know I could use '{ print $1, $2, ... etc}, but this would get bulky.

Also, if I had a small range I did not want to print, is there an elegant way to do this. Something like {print !($100 through $120)}

Thanks,

Matt


 
nawk -v low=15 -v high=20 { for (i=low; i<=high;i++) printf(&quot;%s%s&quot;, $i,(i==high) ? &quot;\n&quot; : OFS) }' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You can define a function like this:
Code:
function PrintRange(imin,imax, i,x){
  for(i=imin;i<imax;++i) x=x$i&quot;&quot;OFS
  x=x$imax
  return x
}
and then call this function like this:
Code:
  print PrintRange(1,99),PrintRange(121,160)

Hope This Help
PH.
 
ooops, you wanted to exclude range - say exclude columns between 100 and 200:

nawk -v low=100 -v high=200 '
{ for (i=1; i<=NF;i++)
if( i>=low && i<=high) continue;
else
printf(&quot;%s%s&quot;, $i,(i!=NF) ? OFS : ORS) }' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top