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 only specific characters in a string...

Status
Not open for further replies.
Feb 14, 2002
88
JP
I've got a kornshell book that explains this quite well at home, but I'm out of town on business, and need to get this done. Not sure if AWK is the way to do it, but I'm basically doing:
ls -lt |awk '{print $9}' to get the file name. Let's say the filename is:
abc65.cf (later we'll have abc66.cf) and so on. I only want to print/use the 65, IE, the numeric characters. For the meantime, it will always be double-digit, although in the near future it'll be up to triple digit. More importantly, the extension is always the same (.cf) and the first part is always 3 alpha characters.

TIA
 
Try:

ls -lt | awk '{gsub("[^0-9]","",$9) ; print $9}'

or

ls -lt | awk '{gsub("^...([0-9]{2,3})\\.cf$","&",$9) ; print $9}' Jean Pierre.
 



ls -t *.cf | awk '{
for( ii = 1; ii <= length( $1); ii++)
if( substr( $1, ii, 1) >= 0 && substr( $1, ii, 1) <= 9 ) {
printf( &quot;%s&quot;, substr( $1, ii, 1))
}
print &quot;&quot;
}'


Regards Gregor Gregor.Weertman@mailcity.com
 
Thanks guys... that stuff looks really familiar in my book, that's sitting at my desk, totally unattended at the moment. :) I'll give that a shot, even though I got the job to a do-able state.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top