LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 12: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, 01:13 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,760

Rep: Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563Reputation: 7563
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, 04:18 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,019

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
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 10:16 PM
matching substrings to multiple part of a line verse123 Linux - Newbie 4 10-30-2011 01:49 AM
sed/awk/grep for multiple line data Coiby Programming 4 09-20-2011 06:16 AM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 04:20 AM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:49 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
Open Source Consulting | Domain Registration