LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help getting multiple substrings from one line using find | grep | sed (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-getting-multiple-substrings-from-one-line-using-find-%7C-grep-%7C-sed-4175412301/)

pnkflydgr 06-19-2012 01:38 PM

Need help getting multiple substrings from one line using find | grep | sed
 
My find | grep is this:
Code:

find ../xml | xargs grep -s '<parameter name="ldapPassword">'
which returns a string like this:
../xml/file.xml:<parameter name="ldapPassword">Password</parameter>

The result I need is this:
file = file.xml
password = Password

This is what I was using to get the password:
Code:

find ../xml | xargs grep -s '<parameter name="ldapPassword">' | sed -e 's/.*ldapPassword">//' -e 's/<\/parameter.*//'
But I'm not sure how to get multiple strings. Well I have an idea of how to do it, but it does not seem like the most efficient. Should I be using Perl instead?

pan64 06-19-2012 02:13 PM

assuming you have only one password entry in a file you can have a grep -r <key> ../xml | sed 'commands' to collect the information. and command will look like:
s/:.*ldapPassword">/\npassword = /; s!</ldapPass.*$!\n!; s/^/file = /;
first will drop everything between : and ldapPassword"> and will insert password = , second will drop the end of the line and third will insert filename =

But I have not tested, just wrote it, I have no source files to check

grail 06-20-2012 05:18 AM

Assuming this is in a bash script and being assigned to variable(s), the first thing is obvious, you would need to assign to an array if only wanting to do a single pass.

The following would assume knowledge of where the items are:
Code:

find ../xml | awk -F"[/:<>]+" '/<parameter name="ldapPassword">/{print $3,$5}'


All times are GMT -5. The time now is 12:28 PM.