Extract the specific tags in a XML file

Why don't you just post the "non-one liner" to start with?

1 Like
Moderator comments were removed during original forum migration.
3 Likes

Hello Scott,

I have learnt by hard way and most of time(almost always) write one liner programs first. Then I will convert them into non-one liner forms and post it, reason is why I don't remove the one-liner form of solution because OP may be working on it.

Thanks,
R. Singh

Hello R.Singh,

Is there a way to repeat the below mentioned code for multiple input files.

awk -F"[><]" '
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3;
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3;
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3
  next
}

Please consider that i have multiple files in one location like below.
For EX:-
1) aqjmsuserDataSource-1696-jdbc.xml
2) aqSample-8170-jdbc.xml
3) DataSourceTest-8855-jdbc.xml

Now, i would like to run the above mentioned script for each file and need to create a separate output file.

the expected output needs to be like below

1) aqjmsuserDataSource-1696-jdbc.properties
2) aqSample-8170-jdbc.properties
3) DataSourceTest-8855-jdbc.properties

Any suggestions please?

Thanks,
Siva

Hello Siv51427882,

Could you please try following and let me know if this helps(Not tested it though).

awk -F"[><]" '
FNR==1{sub(/\..*/,"",FILENAME);file=FILENAME".properties"}
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3; > file
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3;     > file
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3 > file
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3 > file
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3 > file
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3 > file
  next
}'  aqjmsuserDataSource-1696-jdbc.xml   aqSample-8170-jdbc.xml   DataSourceTest-8855-jdbc.xml
 

Thanks,
R. Singh

1 Like

Hello R.Singh,

Getting syntax errors like below.

awk: cmd. line:6: print "DS_Name:"$3; > file
awk: cmd. line:6: ^ syntax error
awk: cmd. line:11: print "URL:"$3; > file
awk: cmd. line:11: ^ syntax error

Thanks
Siva

Hello Siv51427882,

Sorry I forgot to remove ; in above, try following and let me know if this helps you.

awk -F"[><]" '
FNR==1{sub(/\..*/,"",FILENAME);file=FILENAME".properties"}
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3 > file
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3     > file
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3 > file
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3 > file
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3 > file
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3 > file
  next
}'  aqjmsuserDataSource-1696-jdbc.xml   aqSample-8170-jdbc.xml   DataSourceTest-8855-jdbc.xml
 

Thanks,
R. Singh

1 Like

Thank You R.Singh.. That worked like a Gem...!!!

Hello Ravinder Sing,

Please consider below code as input file

<jdbc-system-resource>
    <name>wlsbjmsrpDataSource</name>
    <target>AdminServer,osb_server1</target>
    <descriptor-file-name>jdbc/wlsbjmsrpDataSource-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>aqjmsuserDataSource</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/aqjmsuserDataSource-1696-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>aqSample</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/aqSample-8170-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>DataSourceTest</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/DataSourceTest-8855-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>OSC_WC_JDBCDataSource</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/OSC_WC_JDBCDataSource-8237-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>coe_31machine</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/coe_31machine-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>SampleDBDataSource</name>
    <target>AdminServer,osb_server1,soa_server1</target>
    <descriptor-file-name>jdbc/SampleDBDataSource-9181-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>HEWR Data Source</name>
    <target>AdminServer</target>
    <descriptor-file-name>jdbc/HEWR_Data_Source-8983-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>94machineDB</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/94machineDB-5930-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>FileAdapterDS</name>
    <target>AdminServer,soa_server1</target>
    <descriptor-file-name>jdbc/FileAdapterDS-7294-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>PolycomTest</name>
    <target>AdminServer,soa_server1</target>
    <descriptor-file-name>jdbc/PolycomTest-0055-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>scottDS</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/scottDS-4414-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>IEXPAPPS</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/IEXPAPPS-0246-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>RoboticsDS</name>
    <target>AdminServer</target>
    <descriptor-file-name>jdbc/RoboticsDS-7006-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>

Expected output:-
for each <jdbc-system-resource> tag in above code,
1) The script has to create a new file with a name of '<name>.properties'
2) And in that file, the script has to map parameters like below
target servers = the content of <target></target>
descriptor file name = the content of <descriptor-file-name></descriptor-file-name>

Consider the first tag

<jdbc-system-resource>
<name>wlsbjmsrpDataSource</name>
<target>AdminServer,osb_server1</target>
<descriptor-file-name>jdbc/wlsbjmsrpDataSource-jdbc.xml</descriptor-file-name>
</jdbc-system-resource>

For this snippet, the expected output is like below

1) The script has create a new file called wlsbjmsrpDataSource.properties
2) In the wlsbjmsrpDataSource.properties file,
target servers = AdminServer,osb_server1
descriptor file name = jdbc/wlsbjmsrpDataSource-jdbc.xml
3) The script has to repeat the same for each and every <jdbc-system-resource>

Thanks
Siva

Hello Siv51427882,

I would like to request you to please open a NEW Thread for any new kind of questions because it may be confusing for people. Following may help you in same and let me know in case of any queries.

awk -v s1="target servers = " -v s2="descriptor file name = "  -F"[><]" '
flag && /<\/jdbc-system-resource>/{
  flag="";
  print val1 ORS val2 > file;
  close(file)
}
/<jdbc-system-resource>/{
  flag=1;
  next
}
flag && /<name>/{
  file=$3".properties";
  next
}
flag && /<target>/{
  val1=s1 $3;
  next
}
flag && /<descriptor-file-name>/ && flag{
  val2=s2 $3;
  next
}
'   Input_file
 

Thanks,
R. Singh

1 Like

Sure.
I will create a new thread(s) going forward.
And last script also successfully.
With that being said, this thread can be closed or resolved.

Thanks
Siva