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!

how to substr a string ?

Status
Not open for further replies.

yacobsen

Instructor
May 9, 2012
3
LU
Dear ALL,
I want to extract a string from a file and using the substr function to extract substrings from a string.

I have a file name say "test.dat"
cat test.dat | awk '{if($1=="Latitude") print $6 }'

this produce say "-090206.48241"

the I pipe this directly to

cat test.dat | awk '{if($1=="Latitude") print $6 }' | awk '{ printf("%3i %1i %8.5f\n"),substr($0,1,3),substr($0,4,5),substr($0,5,10)}'

What I wanted this to produce is to extract and output as:
-09 02 06.48241.

The above awk did not do just that ?

Any comments please

Yacob Sen

 
Errata to the question
sorry I meant

cat test.dat | awk '{if($1=="Latitude") print $6 }' | awk '{ printf("%3i %2i %8.5f\n"),substr($0,1,3),substr($0,4,2),substr($0,6,5)}'

it produces: 9 2 6.48000

This misses the "-" and as well as the zeros. I wanted it to produce:
-09 02 06.48000

Regards
Yacob
 
What about this ?
Code:
awk '$1=="Latitude"{x=$6;printf "%03d %02d %08.5f\n",substr(x,1,3),substr(x,4,2),substr(x,6)}' test.dat

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Dea PHV

It worked as I would like it to be. Thank you so much !!
Thank You

Regards
Yacob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top