Solved

with vimgrep - or other vim possibility - how can i get groups of data out of a file ?

Posted on 2015-01-13
4
139 Views
Last Modified: 2015-01-14
Hello every one.

I'm trying to do a vimgrep on a file in order to get only two attributs with their values out of the xml file.
so far i can get the complete lines  and i'd like with the help of groups to have a better display.

:vimgrep key= % | copen

^ I'd like to add to this command the grouping possibility with "\(  \)" ....but without success for the moment.

i'd appreciate your help in order to have a result as explained in the "result  expected"






part of the file that I'm working with :
.....................................................................................................................................................
 <policy name="Menu_Controls" class="User" displayName="$(string.Menu_Controls)" explainText="$(string.IE_ExplainMenu_Controls)" presentation="$(presentation.Menu_Controls)" key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls">
      <parentCategory ref="AdminApproved" />
      <supportedOn ref="SUPPORTED_IE5" />
      <elements>
        <boolean id="MCSiMenu" valueName="{275E2FE0-7486-11D0-89D6-00A0C90C9B67}">
          <trueValue>
            <decimal value="0" />
          </trueValue>
          <falseValue>
            <decimal value="1" />
          </falseValue>
        </boolean>
        <boolean id="PopupMenu_Object" valueName="{7823A620-9DD9-11CF-A662-00AA00C066D2}">
          <trueValue>
            <decimal value="0" />
          </trueValue>
          <falseValue>
            <decimal value="1" />
          </falseValue>
        </boolean>
        <boolean id="Ikonic_Control" valueName="{F5131C24-E56D-11CF-B78A-444553540000}">
          <trueValue>
            <decimal value="0" />
          </trueValue>
          <falseValue>
            <decimal value="1" />
          </falseValue>
        </boolean>
      </elements>
    </policy>
    <policy name="Microsoft_Agent" class="User" displayName="$(string.Microsoft_Agent)" explainText="$(string.IE_Explain_Microsoft_Agent)" presentation="$(presentation.Microsoft_Agent)" key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls">
      <parentCategory ref="AdminApproved" />
      <supportedOn ref="SUPPORTED_IE5" />
      <elements>
        <boolean id="Microsoft_Agent_Control" valueName="{D45FD31B-5C6E-11D1-9EC1-00C04FD7081F}">
          <trueValue>
            <decimal value="0" />
          </trueValue>
          <falseValue>
            <decimal value="1" />
          </falseValue>
        </boolean>
      </elements>
    </policy>
.....................................................................................................................................................



my expected result :
.....................................................................................................................................................
 name="Menu_Controls"  key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls"
 name="Microsoft_Agent" key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls"
......
.....................................................................................................................................................
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 23

Expert Comment

by:savone
ID: 40548209
Here is how I would do it with bash:

#!/bin/bash
while read line
	do name=$(echo $line | grep -oP 'name=[^"]*"\K[^"]*')
	key=$(echo $line | grep -oP 'key=[^"]*"\K[^"]*')
	if [ -z "${name}" ]; then
		continue	
	fi
        echo "name=\"$name\" key=\"$key\""
done < file

Open in new window


Here is the output I received:

# ./do
name="Menu_Controls" key="SoftwarePoliciesMicrosoftWindowsCurrentVersionInternet SettingsAllowedControls"
name="Microsoft_Agent" key="SoftwarePoliciesMicrosoftWindowsCurrentVersionInternet SettingsAllowedControls"
0
 
LVL 23

Expert Comment

by:savone
ID: 40548212
I just noticed it stripped the \ out of the keys.... hmmmm....
0
 
LVL 23

Accepted Solution

by:
savone earned 500 total points
ID: 40548218
Ahh, it seems echo was stripping the backslashes... This works better.

#!/bin/bash
while read -r line
	do name=$(echo $line | grep -oP 'name=[^"]*"\K[^"]*')
	key=$(echo $line | grep -oP 'key=[^"]*"\K[^"]*')
	if [ -z "${name}" ]; then
		continue	
	fi
echo name=\""${name}"\" key=\""${key}"\"
done < file

Open in new window


Output is now:
name="Menu_Controls" key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls"
name="Microsoft_Agent" key="Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\AllowedControls"
0
 

Author Comment

by:toshi_
ID: 40548379
thanks a lot for your help.
wonderful

toshi
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question