Page 1 of 1

Replace text in multiple files with grep sed?

Posted: Mon Jan 16, 2023 5:48 pm
by geo_c

I'm trying to replace a font name and size within all the jwmrc files in the themes directory.

Is it possible to use grep and sed to do this?

It seems the lines I'm coming up with are only designed to pipe filenames to sed. Is there a way to output the contents of the files using grep?

What I have is below, which doesn't work on any level, is trying to replace text within file contents in the directory /DejavuSans-11

I know it's pretty botched, but it's been awhile since I used grep and sed, so I'm really foggy on the syntax particulars.

Code: Select all

grep -rl 'jwmrc' /mnt/home/tbox/jwm/themes-desk/DejavuSans-11 | xargs sed -i 's/DejaVu\ Sans-13\:style\=Book/DejaVu\ Sans-11\:style\=Book/g'

Re: Replace text in multiple files with grep sed?

Posted: Mon Jan 16, 2023 9:54 pm
by Burunduk

I'm not sure what you are trying to achieve. My guess is:
- all the files you want to change are in the directory /DejavuSans-11 and not deeper;
- all the files you want to change are named xxxxx-jwmrc;
- you want to replace the content of all the <Font> tags in them to something uniform.

If this is correct, you can try:
- backup the directory before running the following command;
- cd to it;
- run: sed -i 's!<Font>[^<]*</Font>!<Font>your:font:here</Font>!' *jwmrc
(replace your:font:here with what you need,
for example: sed -i 's!<Font>[^<]*</Font>!<Font>DejaVu Sans-11:style=Book</Font>!' *jwmrc).

If you need something else, please describe it more precisely.


Re: Replace text in multiple files with grep sed?

Posted: Tue Jan 17, 2023 1:29 am
by geo_c
Burunduk wrote: Mon Jan 16, 2023 9:54 pm

I'm not sure what you are trying to achieve. My guess is:
- all the files you want to change are in the directory /DejavuSans-11 and not deeper;
- all the files you want to change are named xxxxx-jwmrc;
- you want to replace the content of all the <Font> tags in them to something uniform.

If this is correct, you can try:
- backup the directory before running the following command;
- cd to it;
- run: sed -i 's!<Font>[^<]*</Font>!<Font>your:font:here</Font>!' *jwmrc
(replace your:font:here with what you need,
for example: sed -i 's!<Font>[^<]*</Font>!<Font>DejaVu Sans-11:style=Book</Font>!' *jwmrc).

If you need something else, please describe it more precisely.

Thanks you nailed it, that's exactly what I'm trying to do. I'll give this a whirl.