Hello,
I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name.
So the file naming convention is: imap-clientname-import-YYYYMMDD.txt
All I am trying to do is: to get the ClientName and Date from the file name. If the Filename does not have dates in their file name, then we put file create date and time.
Also, the output need to be written in a output file with pipe delimitation if possible.
So the output I am looking for is pipe delimitation: clientname and date
hp | 20150917
dell-gec | 20150901
cvs | 20150915
gec | "need a create file date here"
home-depot | "need a create file date here"
I tried something below to get the date. I am getting the desired output. But how to replace it with create date for the files that does not have dates in the filename.
Thank you RudiC for the response. I tried the suggested code but getting the error message below. Also, "la" is not a recognizable command hence changed it to "ls -1" . I am using Sun Solaris. Sorry for not mentioning it earlier.
Also, the date which I trying to replace is the create date of the file and not the sysdate.
ls-1 imap* | awk '
BEGIN {for (n=split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", M); n>0; n--) MNr[M[n]]=n
}
{sub (/\.txt/,"")
n=split ($NF, T, "-")
if (T[n] == "import" || T[n] == "") {n++
T[n]=sprintf ("2015%02d%02d", MNr[$6], $7)}
print T[2] (n==5?"-"T[3]:"") " | " T[n]
}
'
awk: syntax error near line 4
awk: illegal statement near line 4
awk: syntax error near line 8
awk: illegal statement near line 8
Thank you Vgersh99 for the reply.
It is working fine. The only thing I need in this "need a create file date here" to be replaced by that file creation date on unix system in YYYYMMDD format.
ls -1 imap* | nawk -F "(-import[.]*)|([.])" '{print substr($1,index($1,"-")+1),(NF==2)?"need a create file date here":sqrt($(NF-1)^2)}' OFS='|'