LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   accessing array within awk (https://www.linuxquestions.org/questions/programming-9/accessing-array-within-awk-4175411546/)

tinkulim 06-15-2012 01:13 AM

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?

pan64 06-15-2012 01:53 AM

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")

tinkulim 06-15-2012 03:11 AM

I read the file contents into an array

pan64 06-15-2012 03:13 AM

Quote:

Originally Posted by tinkulim (Post 4703826)
I read the file contents into an array

That is fine, can you post the code? And please do not forget to use [code][/code] to keep formatting. Have you checked the examples?






_____________________________________
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")

tinkulim 06-15-2012 03:24 AM

Code:

old_IFS=$IFS;
IFS=$'\n';
ARRAY=($(cat Module_ja.properties));
IFS=$old_IFS

Above is the code I used to copy the contents of file into array.
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.

pan64 06-15-2012 03:40 AM

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")

tinkulim 06-15-2012 03:47 AM

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

grail 06-15-2012 10:15 AM

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.