INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Extract/print the only fields/string included in square brackets

Extract/print the only fields/string included in square brackets

Extract/print the only fields/string included in square brackets

(OP)
Hi All,

as reported in the object I need to extract only all the fields inside this XML file also other included in square brackets not teh other, is there a way ?

[nz@mi-e2bi-prod-3 XML]$ cat Agenti.xml
<ImpactRequest><SearchFor><ModelItem><Path>[CRM - Agenti (Analisi Relational)]</Path></ModelItem></SearchFor><SearchIn><CMSelect>/content/folder[@name=&apos;E2BI&apos;]/folder[@name=&apos;CRM&apos;]</CMSelect></SearchIn><Params><URL>http://e2bireportingco.enelint.global:80/ibmcognos... CMPath="/content/folder[@name=&apos;E2BI&apos;]/folder[@name=&apos;CRM&apos;]/folder[@name=&apos;Package&apos;]/package[@name=&apos;E2BI - CRM&apos;]/report[@name=&apos;A260316 Data Base Agenti&apos;]" Name="A260316 Data Base Agenti" Path="Cartelle pubbliche &gt; E2BI &gt; CRM &gt; Package &gt; E2BI - CRM" Type="Report" linkName="A260316 Data Base Agenti"><Item DirectImpact="1" FndIn="expression" QueryName="Query1">[CRM - Agenti (Analisi Relational)].[Agenti].[Cognome Agente]</Item><Item DirectImpact="1" FndIn="expression" QueryName="Query1">[CRM - Agenti (Analisi Relational)].[Agenti].[Nome Agente]</Item><Item DirectImpact="1" FndIn="expression" QueryName="Query1">[CRM - Agenti (Analisi Relational)].[Agenti].[Login Agente]</Item><Item DirectImpact="1" FndIn="expression" QueryName="Query1">[CRM - Agenti (Analisi Relational)].[Agenti].[Tipo Agente]</Item><Item DirectImpact="1" FndIn="expression" QueryName="Query1">[CRM - Agenti (Analisi Relational)].[Agenti].[Codice Fiscale Agente]</Item></CMObject></Results></ImpactRequest>[nz@mi-e2bi-prod-3 XML]$

Thanks

Bye
Andrea

RE: Extract/print the only fields/string included in square brackets

What exactly does "also other included in square brackets not teh other"

Because it reads as contradictory.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.

RE: Extract/print the only fields/string included in square brackets

(OP)
It means just the fields inside square backets not the other fields (also othter means I have other different XML files).

Thanks.

Ciao
Andrea

RE: Extract/print the only fields/string included in square brackets

I would try to use regular expression for matching strings inside of square brackets - something like this:

andtea.awk

CODE

# Run:
# awk -f andtea.awk andtea.xml

BEGIN {
  all_lines = ""
}

{
  # concatenate all lines into one string
  all_lines = all_lines $0
}

END {
  str = all_lines
  # initialize vars
  delete matches_array
  nr = 0
  # store all matches into an array
  while (str != "" && match(str, /\[[^]]+\]/) > 0 ) {
    matches_array[++nr] = substr(str, RSTART, RLENGTH)
    str = substr(str, RSTART + (RLENGTH ? RLENGTH : 1))
  }
  # print result
  printf("found %2d matches:\n", nr)
  for (i=1; i <= nr; i++) {
    printf("%02d\t%s\n", i, matches_array[i])
  }
} 

Result:

CODE

$ awk -f andtea.awk andtea.xml
found 23 matches:
01	[CRM - Agenti (Analisi Relational)]
02	[@name=&apos;E2BI&apos;]
03	[@name=&apos;CRM&apos;]
04	[@name=&apos;E2BI&apos;]
05	[@name=&apos;CRM&apos;]
06	[@name=&apos;Package&apos;]
07	[@name=&apos;E2BI - CRM&apos;]
08	[@name=&apos;A260316 Data Base Agenti&apos;]
09	[CRM - Agenti (Analisi Relational)]
10	[Agenti]
11	[Cognome Agente]
12	[CRM - Agenti (Analisi Relational)]
13	[Agenti]
14	[Nome Agente]
15	[CRM - Agenti (Analisi Relational)]
16	[Agenti]
17	[Login Agente]
18	[CRM - Agenti (Analisi Relational)]
19	[Agenti]
20	[Tipo Agente]
21	[CRM - Agenti (Analisi Relational)]
22	[Agenti]
23	[Codice Fiscale Agente] 

As you can see I first concatenated all lines of input file into one big string and then used regex on it to extract substrings in square brackets. But I am not sure if this approach would be good for bigger XML files and whether awk is a suitable tool for it at all. Because, if the XML should be very huge, you should first parse only the elements where the strings in square brackets can occur and then use regular expression. In that case (parsing XML + matching regex) I would rather use other tool like Python or Perl.

.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close