简体   繁体   中英

bash egrep replace string with new line

I need replace all occurence of

<?php
<?php

to become just single

<?php

For now I have

egrep --include='*.php' -irl '<?php.*\n*.<?php' ./ | xargs sed -i -e "1s/.*//"

But that egrep replace FIRST line every time when find pattern in whole PHP file.

You can use awk :

awk '!(/^<\?php/ && last ~ /^<\?php/);{last = $0}' input.php

If you want to run that on all files in the current folder use the help of find :

find -name '*.php' -exec bash -c '
    file=$1
    awk "!(/^<\?php/ && last ~ /^<\?php/);{last = \$0} " "$file" > "$file.tmp"
    mv "$file.tmp" "$file"
' - '{}' \;

You can also use the command below (see https://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string for explaination)

$ cat file1
<?php
<?php
totototo
tatata

$ cat file2
<?php
blabalbalbalabla

$ sed '/^<?php/{$!{ N;s/^<?php\n<?php/<?php/;ty;P;D;:y}}' file*
<?php
totototo
tatata

<?php
blabalbalbalabla

Depending on your usecase, this might do the job.

uniq input.php

This will suppress ALL duplicates lines. But it might suffice for your use case.

example :

> cat example.txt 
alice
alice
bob
alice
alice
bob
alice

> uniq example.txt
alice
bob
alice
bob
alice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM