accessing array within awk
I want to print contents of array that contains special characters using awk. Below is the awk code in which I am trying to print array "ARRAY" where "xx" is the index of the array.
But the code below prints ARRAY1, ARRAY2.... instead of printing the contents of array. awk 'BEGIN { xx=0; } { if (index($0, "ns:translate") > 0) { print $0; print "<Name xml:lang=\"sp\"> ARRAY" xx "</Name>"; xx++; } else{ print $0; } }' $1 The contents of the array are: ARRAY: [\u8815S - Perce ~~~~~~~~~ ~~~~~~~~~ ~~\u573 \u8915S - add ~~~~~~~~~ ~~~~~~~~~ ~~\u583 \u8615S - sub ~~~~~~~~~ ~~~~~~~~~ ~~\u573 \u8835S - mult ~~~~~~~~~ ~~~~~~~~~ ~~\u593 \u8865S - div ~~~~~~~~~ ~~~~~~~~~ ~~\u993 .....] The array has close to 100 elements. Can you pls help me accessing the special character array within awk? |
how did you initialize that array? here you can find some practical example
_____________________________________ If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. Happy with solution ... mark as SOLVED (located in the "thread tools") |
I read the file contents into an array
|
Quote:
_____________________________________ If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. Happy with solution ... mark as SOLVED (located in the "thread tools") |
Code:
old_IFS=$IFS; I basically want each line of the file to be added below the line in which the match is found. And enclose the line between "<Name xml:lang=\"sp\"> and </Name> only if the pattern matched line contains ns:translate. |
Sorry, I think is is not awk but bash. You will not be able to handle that array in awk. I do not really understand what you need, but probably something like this:
Code:
awk ' /ns:translate/ { printf "<Name xml:lang=\"sp\">%s</Name>\n", $0 } ' Module_ja.properties _____________________________________ If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. Happy with solution ... mark as SOLVED (located in the "thread tools") |
I have a large file part of which is like below:
<DataGroup xsi:type="ReportDataGroup"> <SmartReportTemplate DescriptionContentType="text/plain" IsActive="true"> <Name ns1:translate="yes">Agent Summary</Name> <Defaults type="defaults"> <Title ns1:translate="yes">Agent Summary Report</Title> <Description ns1:translate="yes"></Description> Now I need to check for the patterns .*ns1:translate="yes">(.*)</.* and when found I need to add string from the array below this line. Along with the string from the array I need to add the tags <Name xml:lang="ja"> and </Name> around the string obt from the array if the line above has </Name> and need to add <Title xml:lang="ja"> and </Title> tags if the pattern matched line has </Title> The final output should look like: <DataGroup xsi:type="ReportDataGroup"> <SmartReportTemplate DescriptionContentType="text/plain" IsActive="true"> <Name ns1:translate="yes">EM - perc</Name> <Name xml:lang="ja">\u886815wEM - perce ~~~~~~~~~ ~~~~~~~~~ ~~\u5834</Name> <Defaults type="defaults"> <Title ns1:translate="yes">AG - Rep</Title> <Title xml:lang="ja">\u886815wAG - Rep ~~~~~~~~~ ~~~~~~~~~ ~~\u5834</Title> <Description ns1:translate="yes"></Description> where the strings "\u886815wEM - perce ~~~~~~~~~ ~~~~~~~~~ ~~\u5834" etc ... are there in an array. I have read contents of file into array. Any idea how to script this? I tried with sed inside a while loop that reads file line by line but it takes a very long time. I tried with awk but I am not able to access the special character array inside awk |
Read your file into an awk array and then use on the second file, something like (not tested):
Code:
awk 'NR==FNR{ARRAY[++i]=$0;next}/ns:translate/{printf("%s\n<Name xml:lang=\"sp\"> %s</Name>",$0,ARRAY[++j])}' Module_ja.properties $1 |
All times are GMT -5. The time now is 07:46 AM. |