Solved AWK string not working

Ibm / P550
November 21, 2012 at 10:47:35
Specs: Sun OS, 10
Hi,

I have a string:
<Target TYPE="oracle_database" NAME="abcdef" DISPLAY_NAME="abcdef">

I want to print only 'abcdef' using awk. I tried :
grep oracle_database |awk '{ print $3}' | awk -F \" '{print $2 }' | grep -i abcdef`;

Looks like not working.

Thanks
Suresh

sureshht


See More: AWK string not working

Report •

#1
November 21, 2012 at 23:24:45
This awk script works because there is a space after each VAR=VALUE which can be defined as a field in awk. The script looks at each field and if it starts with NAME, process the string. Let me know if you have any questions:

#!/bin/ksh

echo '<Target TYPE="oracle_database" NAME="abcdef" DISPLAY_NAME="abcdef">' |
nawk '  # use nawk for Sun/Solaris
{
for(i=1; i <= NF; i++)  # look at each field
   {
   if (match($i, "^NAME") )
      {
      gsub("NAME=","",$i)  # delete the NAME string
      gsub("\"","",$i)             # delete the double quotes
      print $i
      }
   }
}'


Report •

#2
November 22, 2012 at 09:20:09
Hi Nails,

Thank you for your help. I am sorry for not being more clear in my question in the first place. I wanted to get first occurance of 'abcdef' in the line:
<Target TYPE="oracle_database" NAME="abcdef" DISPLAY_NAME="abcdef">.

Thanks again.
Suresh

sureshht


Report •

#3
November 22, 2012 at 23:32:34
✔ Best Answer
I'm not certain what your requirements are. This script searches each field for the first occurrence of "abcdef" and prints out the field:

#!/bin/ksh

echo '<Target TYPE="oracle_database" NAME="abcdef" DISPLAY_NAME="abcdef">' |
nawk '  # use nawk for Sun/Solaris
{
for(i=1; i <= NF; i++) # look at each field
   {
   if (match($i, "abcdef") )
      {
#      gsub("NAME=","",$i) # delete the NAME string
#      gsub("\"","",$i)    # delete the double quotes
      print $i
      exit
      }
   }
}'


Report •

Related Solutions

#4
November 27, 2012 at 08:15:46
Thank you very much Nails

sureshht


Report •

Ask Question