Assistance w/ getting text out of a file
I have a file that has 100 or so lines in this format: dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA How can I strip out everything execpt the servername? Using either shell scripting, sed or perl? Thanks, Ryan
Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
man cut
James, Ryan, On Monday 24 January 2005 13:29, James Knott wrote:
Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
man cut
Cut is limited in its ability to delineate fields. It can use fixed character positions or a specific field separator character, but not a multi-character pattern such as "cn=". Randall Schulz
Randall R Schulz wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn:
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
man cut
Cut is limited in its ability to delineate fields. It can use fixed character positions or a specific field separator character, but not a multi-character pattern such as "cn=".
cat testfile.txt | cut -d, -f1 | cut -d= -f2 If one call of a command doesn't do the trick, just pipe it to another.(^-^) Sandy
Randall R Schulz wrote:
James, Ryan,
On Monday 24 January 2005 13:29, James Knott wrote:
Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
man cut
Cut is limited in its ability to delineate fields. It can use fixed character positions or a specific field separator character, but not a multi-character pattern such as "cn=".
With a bit of manipulation, you can for example, select "=" as the first delimiter, then "," as the next. You'd cut on "=", selecting everything after the cut, then pipe through another cut, using "," and selecting everything before the cut. I did something like that a while ago.
James, Sandy, On Monday 24 January 2005 13:58, James Knott wrote:
Randall R Schulz wrote:
...
man cut
Cut is limited in its ability to delineate fields. It can use fixed character positions or a specific field separator character, but not a multi-character pattern such as "cn=".
With a bit of manipulation, you can for example, select "=" as the first delimiter, then "," as the next. You'd cut on "=", selecting everything after the cut, then pipe through another cut, using "," and selecting everything before the cut. I did something like that a while ago.
What's the point of jumping through hoops with a limited tool when you have a more powerful one that will pay back the effort of understanding its operation with the ability to tackle a much wider range of tasks? We also went through this exercise a few months ago where it turned out that cut is not particularly fast by comparison with sed. Randall Schulz
On Monday 24 January 2005 23:29, James Knott wrote:
Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
man cut
cat file | cut -d':' -f2 | cut -d',' -f1 | cut -d'=' -f2 Paul H -- ____________________________________________________________________ / An American's a person who isn't afraid to criticize the President \ \ but is always polite to traffic cops. / -------------------------------------------------------------------- \ ^__^ \ (@@)\_______ (__)\ )\/\ ||----w | || || -- Paul Hewlett (Linux #359543) Email:`echo az.oc.evitcaten@ttelweh | rev` Tel: +27 21 852 8812 Cel: +27 72 719 2725 Fax: +27 86 672 0563 --
Ryan, On Monday 24 January 2005 13:25, Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
Which field is "the" server name? Assuming it's the "cn=" and that the log file name is "logFile", do this: sed -e 's/.*cn=[^,][^,]*,.*//' logFile Adjust it as necessary if you want a different field.
Thanks, Ryan
Randall Schulz
Randall wrote regarding 'Re: [SLE] Assistance w/ getting text out of a file' on Mon, Jan 24 at 15:37:
Ryan,
On Monday 24 January 2005 13:25, Ryan McCain wrote:
I have a file that has 100 or so lines in this format:
dn: cn=SERVERNAME1,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME2,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA dn: cn=SERVERNAME3,ou=NWSERVERS,ou=SERVER03,ou=iServices,ou=MON,o=LA
How can I strip out everything execpt the servername? Using either shell scripting, sed or perl?
Which field is "the" server name?
Assuming it's the "cn=" and that the log file name is "logFile", do this:
sed -e 's/.*cn=[^,][^,]*,.*//' logFile
Adjust it as necessary if you want a different field.
Are you sure that won't need adjusted regardless of the desired field? :) That'll just erase the whole line, unless your sed is different from mine. You may have meant sed -e 's/.*cn=\([^,][^,]*\),.*/\1/' logfile Or, even better, sed -ne 's/^.*cn=\([^,][^,]*\),.*$/\1/p' logfile I'd also include the line beginning and ending anchors, to explicitly match the whole line (I don't like being ambiguous), and use the -n option so it'll only print matching lines (when paired with the /p after the regex). --Danny, who would've used perl...
Danny, On Monday 24 January 2005 14:45, Danny Sauer wrote:
...
sed -e 's/.*cn=[^,][^,]*,.*//' logFile
Adjust it as necessary if you want a different field.
Are you sure that won't need adjusted regardless of the desired field?
:) That'll just erase the whole line, unless your sed is different
D'Oh!
from mine.
You may have meant sed -e 's/.*cn=\([^,][^,]*\),.*/\1/' logfile Or, even better, sed -ne 's/^.*cn=\([^,][^,]*\),.*$/\1/p' logfile
Yes, though the second form is a distinction without a difference in this case.
I'd also include the line beginning and ending anchors, to explicitly match the whole line (I don't like being ambiguous), and use the -n option so it'll only print matching lines (when paired with the /p after the regex).
--Danny, who would've used perl...
Randall Schulz
participants (6)
-
Danny Sauer
-
James Knott
-
Paul Hewlett
-
Randall R Schulz
-
Ryan McCain
-
Sandy Drobic