Hi all,
In a script like :
job_date=....
ls -l 2>/dev/null |
awk -v var =$job_date '
/Name\.Version\.[0-9]+\.xml$/ {
How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.[0-9]+\.xml$/"
Thanks in advance
Hi all,
In a script like :
job_date=....
ls -l 2>/dev/null |
awk -v var =$job_date '
/Name\.Version\.[0-9]+\.xml$/ {
How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.[0-9]+\.xml$/"
Thanks in advance
just use it in the awk where ever you want
ex:-
($0 ~ var) && /pattern/{do something}
BR
Thanks ahmad.diab,
I want this variable in between the Name and version as shown above..
How to go about that
---------- Post updated at 11:17 AM ---------- Previous update was at 11:16 AM ----------
ex:
Name\.(here comes the variable "var") Version\.[0-9]+\.xml$/
awk -v var=$job_date '
BEGIN { pattern = "Name\\." var "\\.Version\\.[0-9]+\\.xml$" }
$0 ~ pattern {'
Jean-Pierre.
Hi Jean,
i used your code in my script which was also told by you : but the files are not matching the patterns and not getting printed
ls -l 2>/dev/null |
awk -v var=$TOTAL -v var2=$job_date '
BEGIN { pattern = "Name\\." var "\\.Version\\.[0-9]+\\.xml$" }
$0 ~ pattern {
sub(/\.[^.]*$/, "", $NF); # Removes extension
FileCount++;
FileName[FileCount] = $NF;
FileSize[FileCount] = $5;
}
END {
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
print "<AuditFile Version=\"2.0\">";
print "<Header>";
printf "<BusinessDate>%s</BusinessDate>\n",var2
print "<SubmissionSequenceNr>1</SubmissionSequenceNr>"
print "<SubmissionVersionNr>4</SubmissionVersionNr>"
print "<AdHoc>N</AdHoc>"
print "</Header>"
printf "<UnitOfWork UnitSequenceNr=\"1\" FileCount=\"%d\" ArchiveID=\"106B\">\n",var
for (f=1; f<=FileCount; f++) {
print "<DataFile>";
printf "<FileName>%s</FileName>\n", FileName[f];
printf "<FileSize>%d</FileSize>\n", FileSize[f];
print "</DataFile>";
}
}
'
---------- Post updated at 11:44 AM ---------- Previous update was at 11:43 AM ----------
---------- Post updated at 11:44 AM ---------- Previous update was at 11:44 AM ----------
What it the name of the files that you want to proceed ?
Gives us an example.
Jean-Pierre.
Name of the file will be like:
FileName.Version.JobDate.SerialNumber.Constant.xml
Now all the things can be hardcoded, but only Jobdate will vary with time..
This Date is extracted and stored in a variable outside awk in the same script..
I have to use that variable inside awk to make a pattern out of which main xml is getting generated
Please help..
awk -v var=$TOTAL -v var2=$job_date '
BEGIN { pattern = "FileName\\.Version\\." var2 "\\.SerialNumber\\.Constant\\.xml$" }
$0 ~ pattern {
Jean-Pierre.
i already did this, but it was not matching any file name..
are you sure that we will have "$0 ~ pattern" in there..
because above this we are having " ls -l " as suggested by you..for generating the main xml
The ls -l command list all files and the pattern selects the required files.
The files selected by the pattern are : FileName.Version.${job_date}.SerialNumber.Constant.xml
and not *.*.${job_date}.*.*.xml
If you want to select these files, uou must code :
pattern = ".*\\..*\\." var2 "\\..*\\..*\\.xml$"
Jean-Pierre.
Show us real filenames examples that you want to select.
HI Jean,
Can you Please review the below snippet and let me know if its fine ad will solve my purpose :
ls -l 2>/dev/null |
awk -v var=$TOTAL -v var2=$job_date '
BEGIN { pattern = "FileName\\.Version\\." var2 "\\.SerialNumber\\.Constant\\.xml$" }
$0 ~ pattern {
sub(/\.[^.]*$/, "", $NF); # Removes extension
FileCount++;
FileName[FileCount] = $NF;
FileSize[FileCount] = $5;
}
END {
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
print "<AuditFile Version=\"2.0\">";
print "<Header>";
printf "<BusinessDate>%s</BusinessDate>\n",var2
print "<SubmissionSequenceNr>1</SubmissionSequenceNr>"
print "<SubmissionVersionNr>4</SubmissionVersionNr>"
print "<AdHoc>N</AdHoc>"
print "</Header>"
printf "<UnitOfWork UnitSequenceNr=\"1\" FileCount=\"%d\" ArchiveID=\"106B\">\n",var
for (f=1; f<=FileCount; f++) {
print "<DataFile>";
printf "<FileName>%s</FileName>\n", FileName[f];
printf "<FileSize>%d</FileSize>\n", FileSize[f];
print "</DataFile>";
}
}
'
The date is coming up like "20091119" and i also tried following pattern but didnt get any match:
"FileName\\.Version\\.[0-9]+\\.SerialNumber\\.Constant\\.xml$"
thanks for your patience
For the date 20091119 you select only one file :
FileName.Version.20091119.SerialNumber.Constant.xml
I'm not sure that you realy want to do.
If you want to select files all files which name is like :
FileName.Version.JobDate.SerialNumber.Constant.xml
where FileName, Version, SerialNumber and Constant have any value and JobDate has the value contained in the variable var2, you must code :
pattern = ".*\\..*\\." var2 "\\..*\\..*\\.xml$"
Please, show us real filenames examples that you want to select.
Jean-Pierre.
Thanks Jean for quick reply..
Real FileName example would be :
EAGFRU.UK.01.$date.S001.V001.D1.data.xml
EAGFRU.UK.01.$date.S001.V001.D2.data.xml
EAGFRU.UK.01.$date.S001.V001.D3.data.xml
.
.
.
till
EAGFRU.UK.01.$date.S001.V001.D299999.data.xml
here date will be the variable to be used
Thanks
---------- Post updated 11-20-09 at 02:07 AM ---------- Previous update was 11-19-09 at 03:06 PM ----------
hi Jean,
Ur a champ...
Finally it worked..
I was giving some spaces wshich were not required..
Ur pattern worked..
Thanks a ton man