LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2012, 01:13 AM   #1
tinkulim
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Rep: Reputation: Disabled
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?
 
Old 06-15-2012, 01:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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")
 
Old 06-15-2012, 03:11 AM   #3
tinkulim
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
I read the file contents into an array
 
Old 06-15-2012, 03:13 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by tinkulim View Post
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")
 
Old 06-15-2012, 03:24 AM   #5
tinkulim
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-15-2012, 03:40 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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")
 
Old 06-15-2012, 03:47 AM   #7
tinkulim
LQ Newbie
 
Registered: Jun 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-15-2012, 10:15 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk array grob115 Programming 3 10-25-2011 09:19 PM
how to use array in awk rpd2207 Linux - Newbie 3 10-17-2011 07:30 PM
[SOLVED] printing array in awk ghantauke Linux - Newbie 3 11-24-2010 09:16 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration