Using sed and grep/egrep to search and replace

Use this command: egrep -lRZ “\.jpg|\.png|\.gif” . \ | xargs -0 -l sed -i -e ‘s/\.jpg\|\.gif\|\.png/.bmp/g’ egrep: find matching lines using extended regular expressions -l: only list matching filenames -R: search recursively through all given directories -Z: use \0 as record separator “\.jpg|\.png|\.gif”: match one of the strings “.jpg”, “.gif” or “.png” .: start the … Read more

sed multiline delete with pattern

You can use the following: sed ‘/{START-TAG/{:a;N;/END-TAG}/!ba};/ID: 222/d’ data.txt Breakdown: /{START-TAG/ { # Match ‘{START-TAG’ :a # Create label a N # Read next line into pattern space /END-TAG}/! # If not matching ‘END-TAG}’… ba # Then goto a } # End /{START-TAG/ block /ID: 222/d # If pattern space matched ‘ID: 222’ then delete … Read more

Batch script to replace PHP short open tags with

don’t use regexps for parsing formal languages – you’ll always run into haystacks you did not anticipate. like: <? $bla=”?> now what? <?”; it’s safer to use a processor that knows about the structure of the language. for html, that would be a xml processor; for php, the built-in tokenizer extension. it has the T_OPEN_TAG … Read more

Expand variables in sed

Just use double quotes instead of single quotes. You’ll also need to use {} to delimit the number_line variable correctly and escape the \, too. sed -i.bak “${number_line}i\\$var1=$var2” $var3 I’d personally prefer to see all of the variables use the {}, ending up with something like: sed -i.bak “${number_line}i\\${var1}=${var2}” ${var3}

join multiple files

man join: NAME join – join lines of two files on a common field SYNOPSIS join [OPTION]… FILE1 FILE2 it only works with two files. if you need to join three, maybe you can first join the first two, then join the third. try: join file1 file2 | join – file3 > output that should … Read more