LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2012, 01:38 PM   #1
pnkflydgr
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
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?
 
Old 06-19-2012, 02:13 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
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
 
Old 06-20-2012, 05:18 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 11:16 PM
matching substrings to multiple part of a line verse123 Linux - Newbie 4 10-30-2011 02:49 AM
sed/awk/grep for multiple line data Coiby Programming 4 09-20-2011 07:16 AM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 05:20 AM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 12:06 PM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration