awk programming -Passing variable to awk for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk programming -Passing variable to awk for loop
# 1  
Old 08-15-2014
awk programming -Passing variable to awk for loop

Hi All,


I am new to AWK programming. I have the following for loop in my awk program.

cat printhtml.awk:
Code:
     
BEGIN
-------- <some code here>
END{
----------<some code here>
for(N=0; N<H; N++) { for(M=5; M<D[N,0]; M++) print "\t" D[N,M] ""; }
----- }

This "for" loop is part of a large awk program.
This program takes input a simple text file in below format:

Quote:
Column1 Column2 Column3 Column4
Row1 1 2 3 4
Row2 3 4 B ABC
Row3 DEF XYZ 6 8
Row4 Sample Sam1 Sam2 Sam3
The "for" loop prints from 5th line till end of the input file.
I am trying to create a html table using this variable D[N,M].
I want to print every word in a cell so that the final output creates a HTML Table code.
Using the below code I could convert a simple text file to html table but I am not able to put the same logic to my "for" loop.

Code:
awk 'BEGIN{print "<table> <table border=2>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' input_txt.txt > htmloutput.html

I have tried like this:

Code:
        for(N=0; N<H; N++)
        {
                for(M=5; M<D[N,0]; M++) print "<table> <table border=2><tr>";
                for(i=1;i<=NF;i++)
                {
                        print "<td>\t" $i"</td>";print "</tr>" D[N,M];
                }
                print "</table></td>";
        }

How do I pass the variable D[N,M] to a second loop to create a td class?
# 2  
Old 08-15-2014
If you can give us sample input and expected o/p there can be simple ways to achieve desired o/p... so please post sample input and expected o/p for the same..
# 3  
Old 08-15-2014
awk programming -Passing variable to awk for loop

Thanks for the quick reply Akshay.

Sample Input File:

Quote:

Header 1
=====
Line1
Line2
Line3
Line4

Column1 Column2 Column3 Column4
Row1 1 2 3
Row2 3 4 B
Row3 DEF XYZ 6
Row4 Sample Sam1 Sam2


Header 2
=====
Line1
Line2
Line3
Line4

Column1 Column2 Column3 Column4
Row1 1 2 3
Row2 3 4 B
Row3 DEF XYZ 6
Row4 Sample Sam1 Sam2

Using your own inputs from previous posts I could process the lines till "text table [in bold text above]". The text table is delimited by "space".
I need to process the variable "D[N,M]" which are actually table rows.
The value of D[N,M] is :
Column1 Column2 Column3 Column4,
Row1 1 2 3 ,
Row2 3 4 B,
Row3 DEF XYZ 6,
Row4 Sample Sam1 Sam2


for each iteration of the loop.

My output needs to be as below.

awk programming -Passing variable to awk for loop-capture_itjpg

Output HTML code would look like this:
Code:
<table border="1" cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <td width="319" valign="top">
                <p>
                    Header 1
                </p>
            </td>
            <td width="319" valign="top">
                <p>
                    Header 2
                </p>
            </td>
        </tr>
        <tr>
            <td width="319" valign="top">
                <p>
                    Line1
                </p>
                <p>
                    Line2
                </p>
                <p>
                    Line3
                </p>
                <p>
                    Line4
                </p>
            </td>
            <td width="319" valign="top">
                <p>
                    Line1
                </p>
                <p>
                    Line2
                </p>
                <p>
                    Line3
                </p>
                <p>
                    Line4
                </p>
            </td>
        </tr>
        <tr>
            <td width="319" valign="top">
                <table border="1" cellspacing="0" cellpadding="0" width="100%">
                    <tbody>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Column1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column4
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    3
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    4
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    B
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    DEF
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    XYZ
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    6
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row4
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sample
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sam1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sam2
                                </p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
            <td width="319" valign="top">
                <table border="1" cellspacing="0" cellpadding="0" width="100%">
                    <tbody>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Column1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Column4
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    3
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row2
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    4
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    B
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row3
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    DEF
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    XYZ
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    6
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td width="76" valign="top">
                                <p>
                                    Row4
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sample
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sam1
                                </p>
                            </td>
                            <td width="76" valign="top">
                                <p>
                                    Sam2
                                </p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

awk programming -Passing variable to awk for loop-capture_itjpg

Last edited by ctrld; 08-15-2014 at 10:53 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

2. Shell Programming and Scripting

Loop over awk or passing parameter

I wrote this script which works well when I manually input 55518622 and 1 but I need this script to be generic and loop over the following table awk '$4>(55518622-500000) && $4<(55518622+500000)' chr1_GEN2bim | awk 'BEGIN {min=1000000000; max=0;}; {\ if($4<min && $4 != "") min = $4; if($4>max... (8 Replies)
Discussion started by: fat
8 Replies

3. UNIX for Dummies Questions & Answers

Passing Shell Variable to awk

Hello All, May i please why my shell variable is not getting passed into awk script. #!/bin/bash -vx i="1EB07C50" /bin/awk -v ID="$i" '/ID/ {match($0,/ID/);print substr($0,RSTART,RLENGTH)}' /var/log/ScriptLogs/keys.13556.txt Thank you. (1 Reply)
Discussion started by: Ariean
1 Replies

4. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

5. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

awk alias passing a value to a variable

I am trying to turn this into an alias with no luck. I would then like to put the alias into my bashrc file. I know awk is very picky about quotes. I have tried every version of quotes, single quotes, double quotes, and backslashes that I can think of. VAR=$(xrandr | awk '$2=="connected"{s=$1}... (3 Replies)
Discussion started by: cokedude
3 Replies

7. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

8. UNIX for Dummies Questions & Answers

Passing a Shell Variable to awk

Hello, I have a file with 4 columns. An arbitrary example is shown below: a Tp 10 xyz b Tq 8 abc c Tp 99 pqr d Tp 44 rst e Tr 98 efg Based on the values in col 2 and col 3, I will execute another program. I have been running this:... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

9. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies

10. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies
Login or Register to Ask a Question