2

I need a shell script to replace a string pattern in multiple text files.

By pattern I mean like this occurance in multiple files:

CSRF=PG0U-8R3L-KLXT-7UWT-TSRS-IFLY-OI9Y-M75C
CSRF=X67S-3QHA-LNT8-D83N-5BS3-2WA4-7BDP-J1NQ
CSRF=TATJ-3QHA-LNT8-D83N-AGHJ-AGJD-7BDP-JGAJ
CSRF=DHJG-8R3L-AFJH-ASGJ-TSRS-ADGJ-OI9Y-AGHJ

So in general its like CSRF=$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$$-$$$$ where $=any alphanumeric

I already have the sed command to find and replace the old text with the new text. But here i am looking for the string pattern to search and replace.

The command I have is :

    oldtext="CSRF=$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$$-$$$$"
    newtext="CSRF Replaced"    
    find -iname '*.txt' | xargs sed -i "s/$oldtext/$newtext/g"  

So my question is... How do I search for this CSRF String pattern???

4

2 回答 2

0

I dont really understand what you are saying.Perhaps you are looking for this.

find -iname '*.txt' | xargs sed -i "s/^\(CSRF=\)$oldtext/\1 $newtext/g"
于 2012-11-14T07:31:37.350 回答
0
oldtext="CSRF=([a-zA-Z0-9]{4}-){7}[a-zA-Z0-9]{4}"

然后确保调用sed -r(在 Linux 上)或sed -E(在 BSD,例如 Mac OS X 上)使用扩展的正则表达式。

于 2012-11-14T07:35:57.793 回答