awk syntax mistake doubles desired output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk syntax mistake doubles desired output
# 1  
Old 03-07-2012
awk syntax mistake doubles desired output

I am trying to add a line to a BASH shell script to print out a large variable length table on a web page. I am very new to this obviously, but I tried this with awk and it prints out every line twice. What I am doing wrong?

Code:
echo "1^2^3%4^5^6%7^8^9%" | awk 'BEGIN { RS="%"; FS="^"; } {for (i = 1; i<NF; i++)printf("<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>", $1,$2,$3,$1)}'

I included this hard coded text string as an example of the formatted variable data as input to awk. When I test from the command line or in the script it prints out 2 of each output line like this:

Code:
<tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=7></td></tr><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=7></td></tr>

Also for some reason on my system NF returns 1 more than the actual number of records every time I have ever used awk, so that is why I have < rather than <= on this for loop.

TIA for helping someone without a clue.

Last edited by Corona688; 03-07-2012 at 12:19 PM.. Reason: Use [code] ... [/code] tags for code and data, please
# 2  
Old 03-07-2012
Code:
echo "1^2^3%4^5^6%7^8^9" | \
awk 'BEGIN { RS="%"; FS="^"; } {for(i=1;i<=NF;i+=3)printf "<tr><td>%d</td><td>%d</td><td>$%d</td><td align=center><input type=checkbox name=DA value=1>", $1,$2,$3}'

Output:
Code:
<tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=1><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=1>

# 3  
Old 03-07-2012
Feel like an idiot on this one. I actually used to program in C quite a bit, but maybe several days without sleep contributed to my incompetence. I can't believe I was only incrementing by 1 on that loop processing 3 fields!! THANKS for the immediate reply.

NOTE: Still have to use < instead of <= for NF or I get an extra line with no variables, but that is always the case on my system for some reason.
# 4  
Old 03-07-2012
Quote:
Originally Posted by awknewb123
for some reason on my system NF returns 1 more than the actual number of records every time
This is because there is an extra %-sign at the end of the string you are echoing. The 3 %-signs make 4 records, the last one empty...
# 5  
Old 03-07-2012
Even more of an idiot than I thought. I have been appending all my record separators instead of prepending. Man, this is the place to come for quick replies. Thanks again.
# 6  
Old 03-07-2012
Quote:
Originally Posted by awknewb123
[..] I can't believe I was only incrementing by 1 on that loop processing 3 fields!! [..]
Actually you do not need the for loop:
Code:
printf "%s" "1^2^3%4^5^6%7^8^9%" |
awk '{printf("<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>", $1,$2,$3,$1)}' RS=% FS=^

Alternatively, since you are using bash, it could also be done without awk:
Code:
printf "%s" "1^2^3%4^5^6%7^8^9%" | tr % '\n' |
while IFS=^ read f1 f2 f3
do
  printf "<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>" "$f1" "$f2" "$f3" "$f1"
done

or in bash code:

Code:
while IFS=^ read -d% f1 f2 f3
do
  printf "<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>" "$f1" "$f2" "$f3" "$f1"
done <<< "1^2^3%4^5^6%7^8^9%"


Last edited by Scrutinizer; 03-07-2012 at 01:21 PM..
# 7  
Old 03-07-2012
Just tried the prepending record separator with the <= and also your latest awk example with no loop and got this output both times:

Code:
<tr><td></td><td></td><td>$</td><td align=center><input type=checkbox name=DA value=></td></tr><tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>7</td><td>8</td><td>$9


It works if I loop and increment the counter by 3 and use < instead of <= on NF with the trailing % separator for each record. I probably need to stop posting until I get some sleep as my mistakes seem to be compounding themselves at this point. Was trying to meet a deadline. Great help. I'll check back when I am lucid.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545 ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26... (3 Replies)
Discussion started by: deepKrish
3 Replies

2. Shell Programming and Scripting

Output not coming as desired.

Hi guys. I have a file containing some hosts and their IPs. host host1 192.168.2.10 host host2 192.168.2.11 host host3 192.168.2.12 I am writing a script where I want to print these values in 1 line. My script looks like RUNTIME_NODE=`cat hosts.properties | grep host` for i in... (7 Replies)
Discussion started by: Junaid Subhani
7 Replies

3. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

4. Shell Programming and Scripting

Help!! needed to get the desired output

Am in need of your help to get the desired output. nameSECURITY.SERVICES.CONFIG:GETVALUEisPrefetchedNsAccessLast2013-09-13 10:50:13 MESTsAccessTotal1sRunningcHitLastnamePUBLIC.SERVER:INVOKEisPrefetchedNsAccessLast2013-09-17 15:02:05... (5 Replies)
Discussion started by: rocky2013
5 Replies

5. Shell Programming and Scripting

Unable to obtain the desired output

Hi, I am unable to get beyond the exit function. The shell script is used to look for masked files and copy paste them to another location. Please refer to the code below for more information. Thanks Brinjit #!/usr/bin/ksh... (10 Replies)
Discussion started by: brinjit
10 Replies

6. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

7. Shell Programming and Scripting

need to get the desired output

Below is the my cide which is working fine but I am not getting the output indesired format.there is some problem in alignment.Can someone help me to correct this? if ]; then summary=$( echo -e "Please review the log file of auto coloclean utility.\n"; echo -e... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

8. Shell Programming and Scripting

SED - output not desired

echo '0x3f 0xfa ae 0xeA' | sed '/0x/ y/abcdef/ABCDEF/' output: 0x3F 0xFA AE 0xEA echo '0x3f 0xfa ae 0xeA' | sed -r '/0x{2}/ y/abcdefg/ABCDEFG/' output: 0x3F 0xFA AE 0xEA my expected output: 0x3F 0xFA ae 0xEA What I want to achieve is change all hexadecimals to UPPER case(only those... (6 Replies)
Discussion started by: kevintse
6 Replies

9. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies

10. Shell Programming and Scripting

Help me in getting the desired output

I wanted to put "|" this sign at starting and at end of every field but its not working with first field like Currently the out put is : abc | abc | abc | xyz | xyz | xyz | But I want the out put in this form: | abc | abc | abc | | xyz | xyz | xyz | plz help me. (2 Replies)
Discussion started by: akash
2 Replies
Login or Register to Ask a Question