 |
 |
Grep help please
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
silly question: with grep how could I delete all lines of text that contain a word like cat ? In apple script ?
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
Why not use perl?
Code:
perl -i.bak -p -e 's/search/replace/g;' *.html
This will work for all files in the directory with the extension html. Change the extension to what file types you need. It will also create backups of the original files with the suffix .bak.
To search multiple directories use
Code:
find /directory/to/search/ -type f | xargs perl -i.bak -p -e 's/search/replace/g;' *.html
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
Funny you should ask this, too. Because not just five minutes ago I did the same thing to batch change some URLs on my website.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
because my text looks like this
Code:
005 9022_B29 V D 010 01:06:03:13 01:06:03:23 01:00:03:28 01:00:04:08
M2 9022_B29 090.5 01:06:03:13
* FROM CLIP NAME: QUEBRADA 1KM LOADING GEAR.03
* TO CLIP NAME: LENS FLARE SUN.02
* REPAIR: TO SOURCE TRUE SPEED IS 90.483871 FPS
006 9022_B29 V C 01:06:04:13 01:06:04:13 01:00:04:08 01:00:04:08
and every entire line I have that has " Repair" in it needs be be removed. If it can be done in perl I will be happy, does not matter as long as it works with applescript.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
Originally posted by loren s:
because my text looks like this
Code:
005 9022_B29 V D 010 01:06:03:13 01:06:03:23 01:00:03:28 01:00:04:08
M2 9022_B29 090.5 01:06:03:13
* FROM CLIP NAME: QUEBRADA 1KM LOADING GEAR.03
* TO CLIP NAME: LENS FLARE SUN.02
* REPAIR: TO SOURCE TRUE SPEED IS 90.483871 FPS
006 9022_B29 V C 01:06:04:13 01:06:04:13 01:00:04:08 01:00:04:08
and every entire line I have that has " Repair" in it needs be be removed. If it can be done in perl I will be happy, does not matter as long as it works with applescript.
Alright, I understand now. Use this then:
Code:
perl -ni -e 'print unless m/search/' *.txt
This tells pearl to read lines and search for your text, then delete the lines that contain your search string. You can pipe this through xargs from a find as well.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
So you would need something like
Code:
find /directory/ type -f | xargs perl -ni -e 'print unless m/REPAIR/' *.txt
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
like this ?
Code:
[filemaker:~/Public/akm/test] filemake% cd /Users/filemake/Public/akm/testfind /Users/filemake/Public/akm/test/ type -f | okyu perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu: No match.
tcsh: cd: Too many arguments.
[filemaker:~/Public/akm/test] filemake%
[filemaker:~/Public/akm/test] filemake%
I was tring this in terminal, but I had a working script in applescript that would edit the open file of BBedit.
like so
tell application "BBEdit"
activate
replace "^TITLE:.*\\r|^FCM:.*\\r|^M2.*\\r|^\\n+\\*.*\\r|^\ \x1A\\x1A\\x1A\\x1A+.*" using "" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "V\\s+|C\\s+|D\\s+|K\\s+|B\\s+" using "" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace " \\d\\d\\d " using "" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace " \\([A-Z]\\) " using "" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "\\sO\\s" using "" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "([:\\d\\d]) ([\\d\\d:])" using "\\1\\t\\2" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
replace "( +)" using "\\t" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
end tell
(Last edited by loren s; Jun 20, 2004 at 05:40 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
Why do you keep mentioning AppleScript... there are much better tools on MacOS X for this use. My throw into the hat would be sed:
Code:
cat myfile.txt | sed "/Repair/d" > mycleanedFile.txt
where myfile.txt and mycleanedFile.txt have to replaced with the proper names of the files you want.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
Originally posted by loren s:
like this ?
Code:
[filemaker:~/Public/akm/test] filemake% cd /Users/filemake/Public/akm/testfind /Users/filemake/Public/akm/test/ type -f | okyu perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu: No match.
tcsh: cd: Too many arguments.
[filemaker:~/Public/akm/test] filemake%
[filemaker:~/Public/akm/test] filemake%
Well, by using find you don't have to use cd.
Code:
find /Users/filemake/Public/akm/test/ type -f | xargs perl -ni -e 'print unless m/REPAIR/' *.txt
That should do it if that is the directory you want and the file type is .txt
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
Originally posted by larkost:
Why do you keep mentioning AppleScript... there are much better tools on MacOS X for this use. My throw into the hat would be sed:
Code:
cat myfile.txt | sed "/Repair/d" > mycleanedFile.txt
where myfile.txt and mycleanedFile.txt have to replaced with the proper names of the files you want.
I thought people liked apple script < I like it for the instant visual feedback. But it does not matter as long as it is quick and it works.
But doing this
cat okyu.txt | sed "/Repair/d" > mycleanedFile.txt
created a new file but did not clean up the text file,
* REPAIR: TO SOURCE TRUE SPEEIS 90.483871 FPS
does the : in REPAIR: mater ?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
you know I have had a problem with sed before. Can you help me tell if sed is working on my machine ?
10.3.3. Is there a way to test and or repair it ?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
Originally posted by benb:
Well, by using find you don't have to use cd.
Code:
find /Users/filemake/Public/akm/test/ type -f | xargs perl -ni -e 'print unless m/REPAIR/' *.txt
That should do it if that is the directory you want and the file type is .txt
Code:
[filemaker:~] filemake% find /Users/filemake/Public/akm/test/ type -f | okyu.txt perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu.txt: No match.
find: -f: unknown expression primary
[filemaker:~] filemake% find /Users/filemake/Public/akm/test/ type -f | okyu perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu: No match.
find: -f: unknown expression primary
[filemaker:~] filemake%

|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Nov 2002
Location: Far from the internet.
Status:
Offline
|
|
Originally posted by loren s:
Code:
[filemaker:~] filemake% find /Users/filemake/Public/akm/test/ type -f | okyu.txt perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu.txt: No match.
find: -f: unknown expression primary
[filemaker:~] filemake% find /Users/filemake/Public/akm/test/ type -f | okyu perl -ni -e 'print unless m/REPAIR/' *.txt
tcsh: okyu: No match.
find: -f: unknown expression primary
[filemaker:~] filemake%
Oops, I made a typo. It should be -type f
So:
Code:
find /Users/filemake/Public/akm/test/ -type f | xargs perl -ni -e 'print unless m/REPAIR/' *.txt
If the file you want to search and replace is okyu.txt use
Code:
perl -ni -e 'print unless m/REPAIR/' okyu.txt
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
Originally posted by benb:
Oops, I made a typo. It should be -type f
So:
Code:
find /Users/filemake/Public/akm/test/ -type f | xargs perl -ni -e 'print unless m/REPAIR/' *.txt
If the file you want to search and replace is okyu.txt use
Code:
perl -ni -e 'print unless m/REPAIR/' okyu.txt
This deleted all of the text in my file, glad I made a back up 
No wait ! YAY tried again this time this worked ! yaY !Thankyou
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
alright one more please please
for peral or grep or sed,
how can I remove a complete line that begins with a patteren ? I need this one for later.
something like,
* ___ Text cats !
please and thankyou ^v^
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
Quick simple solution
grep -v cats oldcrappyfile > newcoolfile
~BS
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
Originally posted by loren s:
alright one more please please
for peral or grep or sed,
how can I remove a complete line that begins with a patteren ? I need this one for later.
something like,
* ___ Text cats !
please and thankyou ^v^
grep -v -G "^____ Text cats" oldcrappyfile > newcoolfile
(if you want the * escape it with a \ -G "^\*____") <- parenthesis denoting the end of a parenthetical comment, not syntax
~BS
(Last edited by MrBS; Jun 17, 2004 at 07:02 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
Originally posted by MrBS:
grep -v -G "^____ Text cats" oldcrappyfile > newcoolfile
(if you want the * escape it with a \ -G "^\*____")
~BS
Sweet!
But how do I use the last line for the * ?
like this ?
grep -v "^\*____") okyu.txt > newcoolfile.txt
or
grep -v \ -G "^\*____") okyu.txt > newcoolfile.txt
with those I get when trying to remove a line with * . But anything else is prrrrrrfect .
[Doritoss-Computer:Jerry Trip/trinkets/test] ranch% grep -v "^\*____") okyu.txt > newcoolfile.txt
tcsh: Too many )'s.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
If you want to drop lines with the string cat at the beginning, just do this:
grep -v -G ^cat old > new
If you want the string to have a space like this kitty cat, do this:
grep -v -G "^kitty cat" old >new
If you are going to use special characters (of which * is one of), you'll need to
escape them by putting a \ in front, so I like *s (at the beginning of the line again) would be:
grep -v -G "^I like \*s" old > new
~BS
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
sweet happy !
grep -v \* okyu.txt > newcoolfile.txt
Thankyou ^v^ this saved me hours and days of work . !
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
More help please ....
I found these sites while trying to learn this tool. Or any regrex's for that matter.
http://unix.about.com/library/weekly/aa082000a.htm
http://www.microdot.co.uk/grep/
But I am still to stupid to get it all fixxed.
My most current method is driving me nuts.
the grep -v textstuff makes since now to me, but how can I use it to do somthing like
all selected lines that do not have this word find and replace all run of spaces with tabs.
so I would think it is
grep -v REPAIR \s \t oldfile> newhappyfile
but that will not work amoung the various other I tried.
FCM: DROP FRAME
002 BL V D 015 00:00:00:00 00:00:00:15 01:00:18:05 01:00:18:20
M2 5510 031.5 02:17:08:03
* FROM CLIP NAME: XCARET RECREATION A-NIGHT.05
* REPAIR: FROM SOURCE TRUE SPEED IS 31.489141 FPS
003 BL V C 00:00:00:00 00:00:00:00 01:00:19:29 01:00:19:29
 I go cookoo waisting hours still tryinmg this stuff. Also is there enev one GUI program out there for this ? like a simple python something to test really fast if something works or not ?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
pretty please with sugar as a flavor center core , yummy yum ^v^
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
Grep isn't the right tool for editing a file. For cli I'd recommend vi (takes a bit to learn, but nice) or in the gui bbedit has a really nice find/replace functionality.
To do what you want in vi open the file
% vi filename
Then type this
:%s/old/new/g
and hit enter. "old" would be what you want to replace (regex... don't use / either) and "new" is what you want to replace old with.
Here's a nice regex tutorial.
~BS
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2001
Location: Seattle
Status:
Offline
|
|
(oh yeah... if you want to save your file then type
: wq
and then hit enter)
~BS
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jul 2001
Status:
Offline
|
|
Originally posted by MrBS:
Grep isn't the right tool for editing a file. For cli I'd recommend vi (takes a bit to learn, but nice) or in the gui bbedit has a really nice find/replace functionality.
To do what you want in vi open the file
% vi filename
Then type this
:%s/old/new/g
and hit enter. "old" would be what you want to replace (regex... don't use / either) and "new" is what you want to replace old with.
Here's a nice regex tutorial.
~BS
Aw what even more regex's I did not know of ! Boy coders like to make lots of stuff !
Ok I will try this, thankyou . ^v^
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|