Sorry for the off topic, but don't a better resource. I'm not great at scripting, but need a quick script to modify a file.
I have a long file that has lines like this:
some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
And this repeats, but XYZ changes. "DATA" is always called data. (it's being renamed basically)
I need to change the middle line but leave the rest of the file as is like this:
some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
Anyone know a quick way to do this? Any help is appreciated.
Thanks, James
On Sat, May 18, 2013 at 1:15 PM, James Pifer jep@obrien-pifer.com wrote:
Sorry for the off topic, but don't a better resource. I'm not great at scripting, but need a quick script to modify a file.
I have a long file that has lines like this:
some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
And this repeats, but XYZ changes. "DATA" is always called data. (it's being renamed basically)
I need to change the middle line but leave the rest of the file as is like this:
some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
Anyone know a quick way to do this? Any help is appreciated.
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
On 5/18/2013 3:23 PM, Larry Martell wrote:
On Sat, May 18, 2013 at 1:15 PM, James Pifer jep@obrien-pifer.com wrote:
Sorry for the off topic, but don't a better resource. I'm not great at scripting, but need a quick script to modify a file.
I have a long file that has lines like this:
some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
And this repeats, but XYZ changes. "DATA" is always called data. (it's being renamed basically)
I need to change the middle line but leave the rest of the file as is like this:
some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
Anyone know a quick way to do this? Any help is appreciated.
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Larry,
Thanks for the answer. Still having trouble making it work. Been looking at sed for the last two hours. Let me give a specific example of a few lines I would want to change:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
Thanks, James
Am 19.05.2013 um 02:31 schrieb James Pifer jep@obrien-pifer.com:
On 5/18/2013 3:23 PM, Larry Martell wrote:
On Sat, May 18, 2013 at 1:15 PM, James Pifer jep@obrien-pifer.com wrote:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Larry,
Thanks for the answer. Still having trouble making it work. Been looking at sed for the last two hours. Let me give a specific example of a few lines I would want to change:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
$ export file=FILENAME $ for i in $(cat $file) ; do TAG=$(echo $i | cut -d. -f2 |cut -d= -f2) ; echo $i | sed s/CN=/CN=${TAG}_/ ; done
-- LF
Thanks for the answer. Still having trouble making it work. Been looking at sed for the last two hours. Let me give a specific example of a few lines I would want to change:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
Thanks, James
I can't say for sure it'll do what you want, it should be tested extensively to make sure it doesn't destroy your car, sleep with your wife,steal your money, etc etc all the warnings I can give, but I'd suggest perl. this is something I cooked up on a conf call:
[zep@nemesis throwaway]$ cat centoslist CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO [zep@nemesis throwaway]$ cat cent-req #!/usr/bin/perl
open(FH,"< $ARGV[0]") || die "can not open $ARGV[0]:";
while(<FH>){ chomp $_; if($_=~/OU=/){ $prepend=$_; @prepend=split(/.OU=/,$_); $mod=$prepend[1]; $mod=~s/..*//; $_=~s/^CN=/CN=${mod}_/; } print "$_\n"; } [zep@nemesis throwaway]$ perl cent-req centoslist CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO [zep@nemesis throwaway]$
-- Even the Magic 8 ball has an opinion on email clients: Outlook not so good.
On Sat, May 18, 2013 at 6:31 PM, James Pifer jep@obrien-pifer.com wrote:
On 5/18/2013 3:23 PM, Larry Martell wrote:
On Sat, May 18, 2013 at 1:15 PM, James Pifer jep@obrien-pifer.com wrote:
Sorry for the off topic, but don't a better resource. I'm not great at scripting, but need a quick script to modify a file.
I have a long file that has lines like this:
some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
And this repeats, but XYZ changes. "DATA" is always called data. (it's being renamed basically)
I need to change the middle line but leave the rest of the file as is like this:
some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
Anyone know a quick way to do this? Any help is appreciated.
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Larry,
Thanks for the answer. Still having trouble making it work. Been looking at sed for the last two hours. Let me give a specific example of a few lines I would want to change:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
sed only does greedy matching, so you'll have to move to a more modern tool. I'd do this in python. Something like this:
import re, sys
pattern = re.compile('^(CN=)(DATA.OU)(.*?)(..*$)')
for path in sys.argv: with open(path, 'r') as fh: for line in fh: line = line.strip() match = pattern.match(line) if match: print match.group(1)+match.group(3)+'_'+match.group(2)+match.group(3)+match.group(4) else: print line
When I run that with your input I get your desired output.
On 5/19/2013 9:03 AM, Larry Martell wrote:
On Sat, May 18, 2013 at 6:31 PM, James Pifer jep@obrien-pifer.com wrote:
On 5/18/2013 3:23 PM, Larry Martell wrote:
On Sat, May 18, 2013 at 1:15 PM, James Pifer jep@obrien-pifer.com wrote:
Sorry for the off topic, but don't a better resource. I'm not great at scripting, but need a quick script to modify a file.
I have a long file that has lines like this:
some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
And this repeats, but XYZ changes. "DATA" is always called data. (it's being renamed basically)
I need to change the middle line but leave the rest of the file as is like this:
some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
Anyone know a quick way to do this? Any help is appreciated.
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Larry,
Thanks for the answer. Still having trouble making it work. Been looking at sed for the last two hours. Let me give a specific example of a few lines I would want to change:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
sed only does greedy matching, so you'll have to move to a more modern tool. I'd do this in python. Something like this:
import re, sys
pattern = re.compile('^(CN=)(DATA.OU)(.*?)(..*$)')
for path in sys.argv: with open(path, 'r') as fh: for line in fh: line = line.strip() match = pattern.match(line) if match: print match.group(1)+match.group(3)+'_'+match.group(2)+match.group(3)+match.group(4) else: print line
When I run that with your input I get your desired output. _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Thanks Larry! I was able to get it working!!!
James
On Sat, May 18, 2013 at 7:31 PM, James Pifer jep@obrien-pifer.com wrote:
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
So I need to take the data after the FIRST OU and stick in front of DATA with an _ in between. The rest of the line then remains the same.
Hope it makes sense. Appreciate the help!
This should work as long as the first OU always follows CN=DATA.:
sed -e's/DATA.OU=(\w*)/\1_DATA.OU=\1/'
-- Les Mikesell lesmikesell@gmail.com
Oops, I read that too late ...
Let's say my original lines are: CN=DATA.OU=XYZ.O=CO CN=DATA.OU=XYY.OU=MEM.O=CO CN=DATA.OU=XZZ.OU=OOP.O=CO
I want them to look like: CN=XYZ_DATA.OU=XYZ.O=CO CN=XYY_DATA.OU=XYY.OU=MEM.O=CO CN=XZZ_DATA.OU=XZZ.OU=OOP.O=CO
then the perl script would be
perl -pne 's/(CN=)(DATA.OU=)((.*?).O.*)$/$1$4_$2$3/' /file/name
Best regards,
Peter.
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
SCNR T.
Tilman Schmidt wrote:
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
Without even reading the wikipedia piece, I can give my own annoyance with anyone using cat to pipe through sed, or awk, or perl.... To me, that says they don't know their tools.
mark "besides, my cat doesn't like it, either, meeerroowwwww...."
On Thu, May 30, 2013 at 8:37 AM, m.roth@5-cent.us wrote:
Tilman Schmidt wrote:
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
Without even reading the wikipedia piece, I can give my own annoyance with anyone using cat to pipe through sed, or awk, or perl.... To me, that says they don't know their tools.
I am 53 and I have programming since I was 16. I've been using Unix/Linux since 1977 and I've been gainfully employed since 1979 (never at a job where I had to use Windows). So I assure you I know what I am doing.
Larry Martell wrote:
On Thu, May 30, 2013 at 8:37 AM, m.roth@5-cent.us wrote:
Tilman Schmidt wrote:
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
Without even reading the wikipedia piece, I can give my own annoyance with anyone using cat to pipe through sed, or awk, or perl.... To me, that says they don't know their tools.
I am 53 and I have programming since I was 16. I've been using Unix/Linux since 1977 and I've been gainfully employed since 1979 (never at a job where I had to use Windows). So I assure you I know what I am doing.
So why cat | sed?
mark
On Thu, May 30, 2013 at 9:42 AM, m.roth@5-cent.us wrote:
Larry Martell wrote:
On Thu, May 30, 2013 at 8:37 AM, m.roth@5-cent.us wrote:
Tilman Schmidt wrote:
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
Without even reading the wikipedia piece, I can give my own annoyance with anyone using cat to pipe through sed, or awk, or perl.... To me, that says they don't know their tools.
I am 53 and I have programming since I was 16. I've been using Unix/Linux since 1977 and I've been gainfully employed since 1979 (never at a job where I had to use Windows). So I assure you I know what I am doing.
So why cat | sed?
I really don't see why that is a big deal.
On Thu, May 30, 2013 at 10:52 AM, Larry Martell larry.martell@gmail.com wrote:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Because a cat is a terrible thing to waste.
So why cat | sed?
I really don't see why that is a big deal.
It goes back to the days of expensive computing resources when every process mattered (the days of wooden computers and iron programmers...).
Sed or the invoking shell can open files just as easily as cat, so there's no need to set up a pipeline to get a stream of data from a file. Cat isn't adding anything and just takes extra time to load.
-- Les Mikesell lesmikesell@gmail.com
On Thu, May 30, 2013 at 10:30 AM, Les Mikesell lesmikesell@gmail.com wrote:
On Thu, May 30, 2013 at 10:52 AM, Larry Martell larry.martell@gmail.com wrote:
> cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
Because a cat is a terrible thing to waste.
So why cat | sed?
I really don't see why that is a big deal.
It goes back to the days of expensive computing resources when every process mattered (the days of wooden computers and iron programmers...).
Exactly - it mattered once, but doesn't today.
On Thu, May 30, 2013 at 11:43 AM, Larry Martell larry.martell@gmail.com wrote:
> Because a cat is a terrible thing to waste. >
It goes back to the days of expensive computing resources when every process mattered (the days of wooden computers and iron programmers...).
Exactly - it mattered once, but doesn't today.
No, efficiency always matters - it is just that people who can afford to waste resources don't care and people who make commissions on selling the supplies actively discourage it. You probably wouldn't make a pipeline of cat|cat|cat|cat to get input somewhere so why use even one?
-- Les Mikesell lesmikesell@gmail.com
On Thu, May 30, 2013 at 1:01 PM, Les Mikesell lesmikesell@gmail.com wrote:
On Thu, May 30, 2013 at 11:43 AM, Larry Martell larry.martell@gmail.com wrote:
>> Because a cat is a terrible thing to waste. >>
It goes back to the days of expensive computing resources when every process mattered (the days of wooden computers and iron programmers...).
Exactly - it mattered once, but doesn't today.
No, efficiency always matters - it is just that people who can afford to waste resources don't care and people who make commissions on selling the supplies actively discourage it. You probably wouldn't make a pipeline of cat|cat|cat|cat to get input somewhere so why use even one?
+1
One can also make a tangent to someone piping to grep and then piping to awk ... Just use awk to match your regexp!
-- Les Mikesell lesmikesell@gmail.com _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Larry Martell wrote:
On Thu, May 30, 2013 at 9:42 AM, m.roth@5-cent.us wrote:
Larry Martell wrote:
On Thu, May 30, 2013 at 8:37 AM, m.roth@5-cent.us wrote:
Tilman Schmidt wrote:
Am 18.05.2013 21:23, schrieb Larry Martell:
cat file | sed -e's/CN=DATA.OU=(.*).O=CO/CN=\1_DATA.OU=\1.O=CO/'
http://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
Because a cat is a terrible thing to waste.
Without even reading the wikipedia piece, I can give my own annoyance with anyone using cat to pipe through sed, or awk, or perl.... To me, that says they don't know their tools.
I am 53 and I have programming since I was 16. I've been using Unix/Linux since 1977 and I've been gainfully employed since 1979 (never at a job where I had to use Windows). So I assure you I know what I am doing.
So why cat | sed?
I really don't see why that is a big deal.
You, personally, fail to see, that is, don't comprehend, in what manner that, perhaps, might possibly be a pointless and unnecessary, that is, an action performed for no good reason, such as hand-washing a car that one intends to run through a car wash...?
mark
From: James Pifer jep@obrien-pifer.com
I have a long file that has lines like this: some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
I need to change the middle line but leave the rest of the file as is like this: some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
IFS="."; cat file | while read L; do set $L; if [ "$1" = "CN=DATA" ]; then P1=${1#CN=}; P2=${2#OU=}; echo "CN=${P2}_$P1.$2.$3"; else echo $*; fi; done
JD
From: John Doe jdmls@yahoo.com
From: James Pifer jep@obrien-pifer.com
I have a long file that has lines like this: some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4
I need to change the middle line but leave the rest of the file as is like this: some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4
IFS="."; cat file | while read L; do set $L; if [ "$1" = "CN=DATA" ]; then P1=${1#CN=}; P2=${2#OU=}; echo "CN=${P2}_$P1.$2.$3"; else echo $*; fi; done
Oops, if DATA means "DATA": IFS="."; cat file | while read L; do set $L; if [ "$1" = "CN=DATA" ]; then P=${2#OU=}; echo "CN=${P}_DATA.$2.$3"; else echo $*; fi; done
If DATA is generic: IFS="."; cat file | while read L; do set $L; if [ "${1%=*}" = "CN" ]; then P1=${1#CN=}; P2=${2#OU=}; echo "CN=${P2}_$P1.$2.$3"; else echo $*; fi; done
JD
Hi James,
perl -pne 's/^(CN=)(DATA.OU=)((.*?).O=CO)$/$1$4_$2$3/' /file/name
or, if you prefer in-place editing,
perl -i.bak -pne 's/(CN=)(DATA.OU=)((.*?).O=CO)/$1$4_$2$3/' /file/name
which replaces the file with the modified version and keeps a .bak file around for security.
Example:
lagavulin:~ pete$ cat /tmp/test some text some text2 CN=DATA.OU=XYZ.O=CO some text3 some text4 some text some text2 CN=DATA.OU=RST.O=CO some text3 some text4 some text some text2 CN=DATA.OU=ABC.O=CO some text3 some text4 some text some text2 CN=DATA.OU=UVWXYZ.O=CO some text3 some text4
lagavulin:~ pete$ perl -pne 's/(CN=)(DATA.OU=)((.*?).O=CO)/$1$4_$2$3/' /tmp/test some text some text2 CN=XYZ_DATA.OU=XYZ.O=CO some text3 some text4 some text some text2 CN=RST_DATA.OU=RST.O=CO some text3 some text4 some text some text2 CN=ABC_DATA.OU=ABC.O=CO some text3 some text4 some text some text2 CN=UVWXYZ_DATA.OU=UVWXYZ.O=CO some text3 some text4
I guess that's what you need.
Best regards,
Peter.