Hi Gabriel,

Assuming the fields are in that order and there are no double quotes inside any of the values then here's a sed pattern that should do the trick:

sed -e 's/^<USER \(username="[^"]*"\).*\(administrator="[^"]*"\).*\ (authentication_type="[^"]*"\).*\(fullname="[^"]*"\).*>/\1;\2;\3;\4/' bar.txt

Rob


On 02 23, 11, at 4:02 PM, Gabriel Briones wrote:

Hi List,

[1] I have text file called bar.txt that contains the following information and i'm trying to use awk to format the output into a single file separated by semicolon (;).

<USER username="admin" administrator="yes" authentication_type="Local" all_changes="yes" error="yes" id="[email protected] " fullname="FirstName LastName" global_customisation="yes">



[2] My target output is something similar below

username = "admin ";administrator ="yes";authentication_type="Local";fullname="FirstName LastName"



[3] I've created the below awk script called foo.awk and used it against the text file I mentioned in [1] (e.g cat bar.txt | awk -f foo.awk)

{
{ for ( i = 1 ; i <= NF ; i++ )
   if ( $i ~ /username\=\"[a-zA-Z]*\"/ ) username = i
   else if ( $i ~ /administrator\=\"[a-zA-Z]*\"/ ) administrator = i
else if ( $i ~ /authentication_type\=\"[a-zA-Z]*\"/ ) authentication_type = i else if ( $i ~ /fullname\=\"[a-zA-Z]*[[:space:]]*[a-zA-Z]*\"/ ) fullname = i

   }
}
{ print $username";"$administrator";"$authentication_type";"$fullname }


But for some reason, this awk script doesn't provide me the output I want, instead it displays all the contents of the text file. I'm sure it's because if the "fullname" field having a "space" in between. I have tried removing first the "fullname" in my awk script and the output returns the format I want.

Any idea on how to process this to include the "fullname" field using awk?


--

Keyboard error, press F1 to continue
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
http://lists.linux.org.ph/mailman/listinfo/plug
Searchable Archives: http://archives.free.net.ph

_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
http://lists.linux.org.ph/mailman/listinfo/plug
Searchable Archives: http://archives.free.net.ph

Reply via email to