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 at 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/'
From: James Pifer <jep at 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 text4IFS="."; 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
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.