Using a script variable in awk search patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using a script variable in awk search patterns
# 1  
Old 11-19-2009
Using a script variable in awk search patterns

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
# 2  
Old 11-19-2009
just use it in the awk where ever you want
ex:-
Code:
($0 ~ var) && /pattern/{do something}

BR

Last edited by ahmad.diab; 11-19-2009 at 12:14 PM..
# 3  
Old 11-19-2009
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$/
# 4  
Old 11-19-2009
Code:
awk -v var=$job_date '
BEGIN { pattern = "Name\\." var "\\.Version\\.[0-9]+\\.xml$" }
$0 ~ pattern {'

Jean-Pierre.
# 5  
Old 11-19-2009
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 ----------

Quote:
Originally Posted by abhinav192
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:44 AM ----------

Quote:
Originally Posted by abhinav192
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>";
}

}
'
# 6  
Old 11-19-2009
What it the name of the files that you want to proceed ?
Gives us an example.


Jean-Pierre.
# 7  
Old 11-19-2009
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

awk search patterns from file in another

I would like to grep for aaa and bbb and ccc from one line in file1.txt in any order on a line on file2.txt file1.txt aaa bbb ccc ddd fff ggg hhh ddd jjj jjj cccfile2.txt aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt aaa ddd jjj hhh --> no... (1 Reply)
Discussion started by: sdf
1 Replies

4. Shell Programming and Scripting

Search and replace multiple patterns in a particular column only - efficient script

Hi Bigshots, I have a pattern file with two columns. I have another data file. If column 1 in the pattern file appears as the 4th column in the data file, I need to replace it (4th column of data file) with column 2 of the pattern file. If the pattern is found in any other column, it should not... (6 Replies)
Discussion started by: ss112233
6 Replies

5. Shell Programming and Scripting

Multiple patterns for awk script

Hi, I'm getting stuck when supplying multiple patterns for the below code: awk -F, ' .. .. if ($0 ~ pattern) { .. .. } .. .. ' pattern='$ROW' input_file for the same code I'm trying to supply multiple patterns as given below: awk -F, ' .. .. if( ($0 ~ pattern) && ($0 ~... (6 Replies)
Discussion started by: penqueen
6 Replies

6. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

7. Shell Programming and Scripting

Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed. My Existing Text file is Users.txtpaul, Paul Smith, Stevn Smiley, REQ000001, ARS (11) sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11) mike, Mike Conway, Stevn... (8 Replies)
Discussion started by: evrurs
8 Replies

8. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

9. UNIX for Dummies Questions & Answers

script to search patterns inside list of files

>testfile while read x do if then echo $x >> testfile else fi if then echo $x >> testfile else fi done < list_of_files is there any efficient way to search abc.dml and xyz.dml ? (2 Replies)
Discussion started by: dr46014
2 Replies

10. Shell Programming and Scripting

connecting search patterns in awk with AND

Hello friends, I couldnt connect two search patterns in awk, what i want is to search for two words in a log file; "+MB)" and "Done" i use this code /usr/xpg4/bin/awk '/+MB)/ {gsub("\(","",$5);print int($5)}' mylog.txt and i get integer part of (123,45MB) in a log file "mylog" with ... (1 Reply)
Discussion started by: EAGL€
1 Replies
Login or Register to Ask a Question