Search code examples
grepbbedit

Grep for ":" in one instance, but not ":" in another


I need to find : when occurring after an alpha char, but NOT when its before or after an integer, and am having trouble setting that grep.

In the following example, I only want to replace the : after Creation Time with a TAB and NOT where it occurs between numbers...

Creation Time: 10/3/02 6:48:34 PM

I am using BBEdit. Any advice would be gratefully accepted, thank you.


Solution

  • I don't know BBEdit, but this sort of stuff is really easy in Perl. For example:

    echo "Creation Time: 10/3/02 6:48:34 PM" | perl -ne '$_ =~ s/([^\d]):/$1\t/g; print $_'
    

    or if you have a file called stuff.txt with the string in it, you can type:

    perl -ne '$_ =~ s/([^\d]):/$1\t/g; print $_' stuff.txt
    

    The -n flag in perl causes it to loop through a file or STDIN, and the -e part of the options runs the program in the quotes. When looping through the input, the line is stored in the variable $_. So what I'm doing is replacing the contents of $_, matching any part that is not a digit (the part in the parentheses) followed by a : with the matched part ($1 is equal to the part inside the first parentheses) and a tab character. After the replacing, I print the modified $_.

    So you can try

    perl -ne '$_ =~ s/([^\d]):/$1\t/g; print $_' stuff.txt > check.txt
    

    and look at check.txt to make sure I didn't lead you astray, and then copy it over.