Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

extract and format information from a file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-10-2008   -   Original Discussion by sujoy101
sujoy101's Unix or Linux Image
sujoy101 sujoy101 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 5 February 2009, 7:38 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
extract and format information from a file

Hi,


Following is sample portion of the file;

<JDBCConnectionPool DriverName="oracle.jdbc.OracleDriver"
MaxCapacity="10" Name="MyApp_DevPool"
PasswordEncrypted="{3DES}7tXFH69Xg1c="
Properties="user=MYAPP_ADMIN" ShrinkingEnabled="false"
Targets="myapp_server" TestTableName="SQL SELECT 1 FROM DUAL" URL="jdbcLinuxracle:thin:@myserver.net:1521:dbs130"/>


Is there any way (through shell script) I can select information as following :

DriverName=="oracle.jdbc.OracleDriver"
Name=="MyApp_DevPool"
URL="jdbcLinuxracle:thin:@myserver.net:1521:dbs130"

Any suggestion will be highly appreciated.

Thanks

Sujoy
Sponsored Links
    #2  
Old Unix and Linux 04-10-2008   -   Original Discussion by sujoy101
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 19 May 2015, 6:39 AM EDT
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts
if your formatting is clean and as shown:


Code:
#  awk '{print $NF}' infile | egrep "Name|URL" | sed 's#/>$##'
DriverName="oracle.jdbc.OracleDriver"
Name="MyApp_DevPool"
URL="jdbcracle:thin:@myserver.net:1521:dbs130"

Sponsored Links
    #3  
Old Unix and Linux 04-10-2008   -   Original Discussion by sujoy101
era's Unix or Linux Image
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
If we can assume that you are only interested in attributes within the file, and that they are always in double quotes, how about this.

1. Convert the file to one attribute per line
2. Grep the ones you want from that


Code:
sed -e 's%/>%%' -e 's/\(^\| \)\([A-Za-z]*="[^"]*"\)/\
\2/g' phile.xml | egrep '^(DriverName|Name|URL)='

If your sed can't handle a literal newline (yes, that's slash, backslash, newline, \2/g, in the wrap between the first and second line) then it's a bit tricky. Some seds also understand \n to mean a literal newline in the substitution part.

Tytalus' solution assumes your fields will always be the final field on a line, which sounds kind of precarious. (Also awk | grep is Useless; awk is perfectly capable of taking care of most of what grep can do.)
    #4  
Old Unix and Linux 04-10-2008   -   Original Discussion by sujoy101
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline
Registered User
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 Posts
I assume that the snippet you provided is part of a valid well-formed XML document. If so, the following XSL stylesheet will transform the XML document into the output you want and is a better solution than using sed/awk/etc.


Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

<xsl:template match="JDBCConnectionPool">
DriverName=="<xsl:value-of select="@DriverName"/>"
Name=="<xsl:value-of select="@Name"/>"
URL=="<xsl:value-of select="@URL"/>"
</xsl:template>

</xsl:stylesheet>


Code:
$ xsltproc file.xsl file.xml
DriverName=="oracle.jdbc.OracleDriver"
Name=="MyApp_DevPool"
URL=="jdbcracle:thin:@myserver.net:1521:dbs130"
$

Sponsored Links
    #5  
Old Unix and Linux 04-15-2008   -   Original Discussion by sujoy101
sujoy101's Unix or Linux Image
sujoy101 sujoy101 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 5 February 2009, 7:38 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Murphy,

Thanks a lot for your xsl. Yes its prefectly feching the required info.
If you can pls provide following clarifications:

1.the output is consisting of lot of spaces in between two results.

2. How to insert the "filename" with every result in attach output; so the detail for specific file can be identified.

3.If I wish to add one more "template match" section within the same xsl ; how to do that?

regards

Sujoy
Attached Files
File Type: txt JDBC_data.txt (84.4 KB, 13 views)
Sponsored Links
    #6  
Old Unix and Linux 04-15-2008   -   Original Discussion by sujoy101
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 5 December 2017, 5:22 PM EST
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
This should give you the desired output:


Code:
awk 'BEGIN{FS="\""; printf("Filename= %s\n\n", FILENAME)}
$1 ~ /.* DriverName=$/{print "DriverName==" FS $2 FS}
$3 ~ /.* Name=$/{print "Name==" FS $4 FS}
$5 ~ /.* URL=$/{print "URL==" FS $6 FS ;print ""}
' file

Regards
Sponsored Links
    #7  
Old Unix and Linux 04-15-2008   -   Original Discussion by sujoy101
sujoy101's Unix or Linux Image
sujoy101 sujoy101 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 5 February 2009, 7:38 AM EST
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Franklyn,

I have modified the same with

awk 'BEGIN{FS="\""; printf("Filename= %s\n\n", /usr/data/weblogic/config/mktmixDomain/config.xml)}
$1 ~ /.* DriverName=$/{print "DriverName==" FS $2 FS}
$3 ~ /.* Name=$/{print "Name==" FS $4 FS}
$5 ~ /.* URL=$/{print "URL==" FS $6 FS ;print ""}
' file


but it gives following error:

./new.sh
awk: syntax error near line 1
awk: illegal statement near line 1

where am I going wrong?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract various information from a log file SilvesterJ Shell Programming and Scripting 4 09-27-2011 01:00 PM
extract information from a log file (last days) matarsak Shell Programming and Scripting 14 09-19-2011 04:23 PM
Create shell script to extract unique information from one file to a new file. Mr_47 Shell Programming and Scripting 16 09-11-2011 02:33 PM
Extract information from Log file formatted dummie55 Shell Programming and Scripting 3 03-11-2011 03:30 PM
How to extract a piece of information from a huge file Marcor Shell Programming and Scripting 2 03-13-2008 05:33 PM



All times are GMT -4. The time now is 10:05 PM.