You are not logged in.

#1 2012-01-09 19:11:15

Viper_Scull
Member
From: London, UK
Registered: 2011-01-15
Posts: 153

Using sed in a script

Hi. I would like towrite a script to search in a group of files for a pattern (in this case an specific word), and delete the lines containing that word.

Can anyone give me a hand?

thanks!


Athlon II X4 620 + Gigabyte 785GPM-UD2H + 4GB DDR3 + SSD OCZ Vertex2 60GB

Archlinux x86_64 + Openbox

Offline

#2 2012-01-09 19:21:59

dodo3773
Member
Registered: 2011-03-17
Posts: 818

Re: Using sed in a script

If you are searching for something are you sure it is sed that you want to use? Could you give an example and provide a little more information into what you are trying to do?

Edit: I should have read the original post more carefully.

Last edited by dodo3773 (2012-01-09 19:33:21)

Offline

#3 2012-01-09 19:22:30

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using sed in a script

# delete lines matching pattern
sed '/pattern/d'

Edit:

[karol@black foo1]$ cat a
foo
bar
baz
[karol@black foo1]$ cat b
foo bar baz
bar
foo
baz
[karol@black foo1]$ cat c
foo
bar
foo
baz
[karol@black foo1]$ sed -i '/foo/d' $(find . -type f)
[karol@black foo1]$ cat a b c
bar
baz
bar
baz
bar
baz

Last edited by karol (2012-01-09 19:30:48)

Offline

#4 2012-01-09 19:37:10

Viper_Scull
Member
From: London, UK
Registered: 2011-01-15
Posts: 153

Re: Using sed in a script

I thought sed would be the easiest way to do it. Maybe perl would do it as well..

The idea would be something like this

for file in path
read every line.
if line matches pattern then delete line

Edit: Karol i just saw your editing. Thank you, i think that's what i needed.

Last edited by Viper_Scull (2012-01-09 19:42:17)


Athlon II X4 620 + Gigabyte 785GPM-UD2H + 4GB DDR3 + SSD OCZ Vertex2 60GB

Archlinux x86_64 + Openbox

Offline

#5 2012-01-09 23:44:13

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Using sed in a script

Please don't use find like that, it's not always going to do what you want.

find . -type f -exec sed -i '/foo/d' {} +

Offline

#6 2012-01-09 23:48:26

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using sed in a script

falconindy wrote:

Please don't use find like that, it's not always going to do what you want.

find . -type f -exec sed -i '/foo/d' {} +

Of course you're right, "my way" didn't even look right, but I've missed a typo when trying to do it the correct way. Thanks.
Need ... more ... coffee ...

Offline

#7 2012-01-12 21:03:46

Viper_Scull
Member
From: London, UK
Registered: 2011-01-15
Posts: 153

Re: Using sed in a script

Is the sintaxis of

find . -type f -exec sed -i '/foo/d' {} +

correct? It won't work on my pc like that.

What's the + for?

Last edited by Viper_Scull (2012-01-12 21:04:41)


Athlon II X4 620 + Gigabyte 785GPM-UD2H + 4GB DDR3 + SSD OCZ Vertex2 60GB

Archlinux x86_64 + Openbox

Offline

#8 2012-01-12 23:53:46

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using sed in a script

Yes, this line works.
What do you mean by "It won't work on my pc like that"? Are you getting any errors?
You should of course replace 'foo' with the phrase you want to remove. What's the word you want to get rid of?

Offline

#9 2012-01-13 00:21:16

Viper_Scull
Member
From: London, UK
Registered: 2011-01-15
Posts: 153

Re: Using sed in a script

Never mind. It's working now. I wasnt getting any error earlier just the propmt didnt show up like if it was expecting more arguments.

Thanks for your help.

I'm trying to understand the script.
what's the brackets and the + for?

Last edited by Viper_Scull (2012-01-13 00:21:50)


Athlon II X4 620 + Gigabyte 785GPM-UD2H + 4GB DDR3 + SSD OCZ Vertex2 60GB

Archlinux x86_64 + Openbox

Offline

#10 2012-01-13 00:23:23

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using sed in a script

Viper_Scull wrote:

what's the brackets and the + for?

Which part of 'man find' seems unclear? :-)

Offline

Board footer

Powered by FluxBB