OT: Script Help

Home » CentOS » OT: Script Help
CentOS 21 Comments

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

21 thoughts on - OT: Script Help

  • 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:

  • Am 19.05.2013 um 02:31 schrieb James Pifer :

    $ 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

  • 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

  • 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)
    print line

    When I run that with your input I get your desired output.

  • 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.


    lagavulin:~ pete$ cat /tmp/test some text some text2

  • Oops, I read that too late …

    then the perl script would be

    perl -pne ‘s/(CN=)(DATA\.OU=)((.*?)\.O.*)$/$1$4_$2$3/’ /file/name

    Best regards,


  • Tilman Schmidt wrote:
    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….”

  • 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.

  • 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.

  • 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?

  • Larry Martell wrote:

    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…?


  • +1

    One can also make a tangent to someone piping to grep and then piping to awk … Just use awk to match your regexp!