Sponsored Content
Full Discussion: awk print in script
Top Forums Shell Programming and Scripting awk print in script Post 302976362 by ocbit on Wednesday 29th of June 2016 12:30:53 PM
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>

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 01:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy