awk print in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print in script
# 1  
Old 06-22-2016
awk print in script

Hi, Thanks to RudiC I have a functioning awk portion of a script which reads a text file and replaces all matching values in an XML.

I need help with placing print statements in the script to see line by line what is being replaced.

Script:
Code:
#!/bin/sh

if [ $# -eq 2 ]
  then
        echo
else
    echo "usage: $0 <Properties> <DeploymentXML>"
    exit
fi

TXT=$1
XML=$2

awk '
FNR == NR       {sub (/\./, "/", $TXT)
                 T[$TXT] = $XML
                 next
                }
                {for (t in T) if ($0 ~ t) TF = t
                }
TF && /<value/  {sub (/>[^<]*</, ">" T[TF] "<")
                 TF = ""
                }
TF && /<machine/{sub (/>[^<]*</, ">" T[TF] "<")
                 TF = ""
                }
1
' FS="~" $TXT $XML > temp &&  mv temp $XML

For example:
Text file contains multiple entries in the format of -


Code:
Application.Env~DEV 
Application.ID~99999

Pre-script XML contains multiple entries in the format of -

Code:
<name>Application/Env</name> 
<value>XXX</value> 
<name>Application/ID</name> 
<value>00000</value>

Post-script XML:

Code:
<name>Application/Env</name> 
<value>DEV</value> 
<name>Application/ID</name> 
<value>99999</value>

Thanks!
# 2  
Old 06-22-2016
I readily believe the RudiC helped you create an awk program that could do something similar to what you are describing... But I can't believe that the code you have shown us does anything like what you are describing. (Shell variables are not expanded inside single-quoted strings. You are using code that changes data found in <value> tags and data found in <machine> tags, but your description doesn't say anything about changing data in <machine> tags.

And, since the code shown copies entire input files to corresponding output files (possibly after updating some text), I don't understand what additional text you are hoping to produce nor where you want that additional text to be written???

Please give us a much clearer description of what you are trying to do and show us small representative samples of an input file and the corresponding output file (or files) you hope to produce from that input. (And, be sure that the code that you have shown us does produce the output that you have indicated it currently produces.)
# 3  
Old 06-23-2016
The code I provided works perfectly fine as I posted it. Why would I post it and say it works if it doesn't? Replacing <machine> data is my addition to it, but that is not relevant to this post.

The original code is:

Code:
awk '
FNR == NR       {sub (/\./, "/", $1)
                 T[$1] = $2
                 next
                }
                {for (t in T) if ($0 ~ t) TF = t
                }
TF && /<value/  {sub (/>[^<]*</, ">" T[TF] "<")
                 TF = ""
                }
1
' FS="~" flatfile xmlfile

Currently, the code takes a text input file with format
Code:
Application.Env~DEV 
Application.ID~99999
Application.Name~appname

along with an XML input file with format
Code:
<name>Application/Env</name> 
<value>TST</value> 
<name>Application/ID</name> 
<value>00000</value>
<name>Application/Name</name> 
<value>Name</value>

and replaces <value> data with the data found after the ~ in the text file.
So afterwards it looks like
Code:
<name>Application/Env</name> 
<value>DEV</value> 
<name>Application/ID</name> 
<value>99999</value>
<name>Application/Name</name> 
<value>appname</value>

What I'm now asking is - Where would I put print commands in this script to show on the command line what is being replaced line by line?
# 4  
Old 06-23-2016
Quote:
Originally Posted by ocbit
The code I provided works perfectly fine as I posted it.
Sorry, impossible. Inside awk, TXT and XML are uninitialized variables, thus empty, and can't be used neither as a index into an array nor as a reasonable assignment value. Plus, with the leading $-sign, their contents will be interpreted as a field identifier and MUST be numeric. Again, leading nowhere.
Outside, they are just filenames, aren't they, so nothing to replace anything?
Quote:
Why would I post it and say it works if it doesn't?
Not sure...?

For your original question, place a print just before and just after the replacement statements to see the difference.
# 5  
Old 06-23-2016
To see what is getting replaced you can use match function and print, write output to another file:-
Code:
awk -F'[~><]' '
        FNR == NR {
                sub ( /\./, "/", $1 )
                T[$1] = $2
                next
        }
        /<name>/ {
                name = $3
        }
        /<value>/ {
                if ( name in T )
                {
                        match ( $0, />[^<]*</ )
                        print "Replacing: ", substr ( $0, RSTART, RLENGTH ) " with >" T[name] "<"
                        sub ( />[^<]*</, ">" T[name] "<" )
                }
        }
        {
                print $0 > "new.xml"
        }
' flatfile xmlfile

This User Gave Thanks to Yoda For This Post:
# 6  
Old 06-29-2016
Yoda, thank you. Exactly what I was after.

---------- Post updated 06-29-16 at 12:30 PM ---------- Previous update was 06-28-16 at 04:15 PM ----------

Yoda, a follow up question on the code:

I put print statements throughout the code and am seeing strings with 2 tokens such as Filesystem.FileLoc are being replaced, whereas strings with more than 2 tokens like Connections.JDBC.XX.USER.NAME are not. I realize the sample I posted previously had 2 tokens but there are possibilities of having more.

What should be done to handle more than 2 tokens? Also where does the $3 variable come from?

This is the code with print in it:
Code:
awk -F'[~><]' '
        FNR == NR {
                sub ( /\./, "/", $1 )
                T[$1] = $2
                print "T array: " T[$1]
                next
        }
        /<name>/ {
                name = $3
                print "Name: " name
        }
        /<value>/ {
                if ( name in T )
                {
                        print "name: " name " t: " T
                        match ( $0, />[^<]*</ )
                        print "Replacing: ", substr ( $0, RSTART, RLENGTH ) " with >" T[name] "<"
                        sub ( />[^<]*</, ">" T[name] "<" )
                }
        }
        {
                print $0 > "new.xml"
        }
' flatfile xmlfile

Cmd Line Output:
Code:
T array: Local
T array: UName
T array: PWord
T array: /home/THEfile/
T array: /home/THEtrigger/
Name: Connections/JDBC/Install
Name: Connections/JDBC/XX/USER/NAME
Name: Connections/JDBC/XX/PASSWORD
Name: Filesystem/FileLoc
name: Filesystem/FileLoc t:
Replacing:  >/home/file/< with >/home/THEfile/<
Name: Filesystem/TriggerFile
name: Filesystem/TriggerFile t:
Replacing:  >/home/trigger/< with >/home/THEtrigger/<

new.xml
Code:
<root>
<name>Connections/JDBC/Install</name>
<value>Remote</value>
<name>Connections/JDBC/XX/USER/NAME</name>
<value>name</value>
<name>Connections/JDBC/XX/PASSWORD</name>
<value>pwd</value>
<name>Filesystem/FileLoc</name>
<value>/home/THEfile/</value>
<name>Filesystem/TriggerFile</name>
<value>/home/THEtrigger/</value>
</root>

flat file:
Code:
Connections.JDBC.Install~Local
Connections.JDBC.XX.USER.NAME~UName
Connections.JDBC.XX.PASSWORD~PWord
Filesystem.FileLoc~/home/THEfile/
Filesystem.TriggerFile~/home/THEtrigger/

xml file:
Code:
<root>
<name>Connections/JDBC/Install</name>
<value>Remote</value>
<name>Connections/JDBC/XX/USER/NAME</name>
<value>name</value>
<name>Connections/JDBC/XX/PASSWORD</name>
<value>pwd</value>
<name>Filesystem/FileLoc</name>
<value>/home/fileloc/</value>
<name>Filesystem/TriggerFile</name>
<value>/home/triggerfile/</value>
</root>

# 7  
Old 06-29-2016
Replace first sub function in the code with gsub
Code:
   FNR == NR {
               gsub ( /\./, "/", $1 )

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to match string and print status

Dear team, Need support to built awk script for below requirement Input file LOTC cluster state: ------------------- Node safNode=SC_2_1 joined cluster | Node safNode=SC_2_2 joined cluster | Node safNode=PL_2_3 fail cluster | AMF cluster state: ------------------... (16 Replies)
Discussion started by: shanul karim
16 Replies

2. Shell Programming and Scripting

awk script to print file name

I have the following awk script that looks at the first 2 columns of multiple files and when they match, it prints the min of the 3rd column. for year in tave-{1950..2015}.txt do cat "$year" done | awk '{n=$3; $3=x} !($0 in A) || n<A {A=n} END{for(i in A) print i A}' > output.txt ... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

awk script to match and print

I need a script that will search for a string from column 1 in file A and when the string matches the last column in file B, print columns 1, 2 (file A) and columns 2, 3 (file B). input file A stringtomatch1 a stringtomatch2 a stringtomatch3 b file B junkcolumn1 printcolumn2... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

4. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

5. Fedora

Shell Script - awk, begin, for and print

pointsb=`awk -v a2="$a2" -v b2="$b2" -v c2="$c2" -v yb="$yb" -v yc="$yc" \ 'BEGIN { for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y }; }'` I am learning shell script. I was reading a script and got confused in this line. I understood that awk is allowing to assign the variable. But... (10 Replies)
Discussion started by: agriz
10 Replies

6. Shell Programming and Scripting

How to print backslash in shell script using awk?

I found that echo "aaa" | awk '{print ",\\";}' works, and it will give "\". but ddd=`echo "aaa" | awk '{print ",\\";}'`; echo $ddd will not work. Could anyone tell me why? thank you. (8 Replies)
Discussion started by: wxuyec
8 Replies

7. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

8. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

9. Shell Programming and Scripting

awk/shell script to print each line to a file

Dear People, My query is: have a file, which looks likes this: 10 20 30 40 50 1 2 3 4 5 100 200 300 400 500 what i need is: "PRINT EACH LINE TO AN UNIQUE FILE" desired output: file 1 10 20 30 40 50 file 2 1 2 3 4 5 (3 Replies)
Discussion started by: saint2006
3 Replies

10. Shell Programming and Scripting

AWK script to print all the columns excpet the one specified

I have several columns by the name A B C D E...... and I want to print all the column other than column C and D. Could you please help me with the awk script? Thanks!! (3 Replies)
Discussion started by: kn.naresh
3 Replies
Login or Register to Ask a Question