LM528
LM528

Reputation: 9

bash script to edit all files in a dir

trying to edit all log files from a chat dir to replace a string value found on the next line after a given value.

The code below works:

sed '/Did cool stuff/{n;s/how cool/sweet as bru/g} logname.log

But when attempting to exec bash it never completes, i.e. just hangs:

#!home/bin/bash
for file in /home/logs/*.log ; do
    sed '/Did cool stuff/{n;s/how cool/sweet as bru/g}' ;
done

Feel like I'm missing something obvious.

Upvotes: 0

Views: 1516

Answers (1)

Jasen
Jasen

Reputation: 12432

For the for loop you need the loop variable on the command line, if you leave the filename off sed will read from standard input, and the script will hang waiting for input.

#!/bin/bash
for file in /home/logs/*.log ; do
    sed '/Did cool stuff/{n;s/how cool/sweet as bru/g}' "$file";
done

But for is not needed, sed can do that already.

sed '/Did cool stuff/{n;s/how cool/sweet as bru/g}' /home/logs/*.log

If you want to alter the file content instead of display the altered version use -i.

sed -i '/Did cool stuff/{n;s/how cool/sweet as bru/g}' /home/logs/*.log

Upvotes: 5

Related Questions