Replace text in multiple files with grep sed?

interpretive language scripts


Moderator: Forum moderators

Post Reply
geo_c
Posts: 2880
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2202 times
Been thanked: 876 times

Replace text in multiple files with grep sed?

Post 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'

geo_c
Old School Hipster, and Such

Burunduk
Posts: 252
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 127 times

Re: Replace text in multiple files with grep sed?

Post 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.

geo_c
Posts: 2880
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2202 times
Been thanked: 876 times

Re: Replace text in multiple files with grep sed?

Post 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.

geo_c
Old School Hipster, and Such

Post Reply

Return to “Scripts”