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!

Using AWK to get part of filename

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi,

First question: can I use awk in a Fortran program?
Second: I'd like to use substr to get the part of a file itself (not the contents of the file). For example, I have many files in a directory with the same prefix and a YYMMDD.dat suffix. I want to pick out the YY part of the filename and assign it to a variable, say yr.

Would it look something like (if I'm in the proper directory):

yr = "awk ' { print substr(FILENAME, 18, 2) } '" ?

There are many filenames I'd like to do for this process.

Am I asking too much of awk and Fortran?
 
I'm no FORTRAN wizard, so I don't know what facilities it has for calling external programmes, if any.

There are various ways you can obtain the year from the filename in shell though:

Code:
filename=080916.dat
# with awk
yr=$(echo $filename | awk '{print substr($0,1,2)}')
# with cut
yr=$(echo $filename | cut -c1-2
# with sed
yr=$(echo $filename | sed 's/\(..\).*/\1'/)
# with shell builtin suffix removal
yr=${filename%????.dat}

Note that $( ... ) is interchangeable with ` ... ` on most shells, but I prefer to former due to its "nestability".



Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top