1

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.

hd84335
  • 8,815
  • 5
  • 34
  • 45

1 Answers1

2

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.

  • Thanks very much indeed for the chime-in. However, when I cd to the folder where a test file lives, and run the above string as "perl -ne '$_ =~ s/([^\d]):/$1\t/g; print $_' 000001.txt" (without the quotes), there is odd output in OSX Terminal (a portion of the text in one of the lines in the file). Moreover, I am unsure (not knowing Perl) where I would input or alter to allow for a TAB to be inserted after the alphachar-plus-colon. ?? – Timothy Hellum Jan 09 '18 at 21:55
  • So perl by itself will print it's output to the terminal. If you follow that string by `> test.txt`, the output will instead go into `test.txt`. Then you can look at the file to make sure it's right before moving it to `000001.txt`. You can look at [this, for example](https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file). – Daniel Abercrombie Jan 09 '18 at 22:05
  • I guess long story short, try `perl -ne '$_ =~ s/([^\d]):/$1\t/g; print $_' 000001.txt > test.txt` and let me know what's in `test.txt`. (Note, this will overwrite `test.txt` if it already exists, so pick a different name if you don't want to do that.) – Daniel Abercrombie Jan 09 '18 at 22:16
  • That absolutely did the trick! Thank you very much indeed for your assistance. The resulting text.txt opened in Excel with data in appropriate columns as expected. – Timothy Hellum Jan 09 '18 at 22:52
  • 1
    BBEdit uses PCRE so that regular expression should work in the app too. – steveax Jan 10 '18 at 17:38