sed + remove “#” and empty lines with one sed command

If you’re worried about starting two sed processes in a pipeline for performance reasons, you probably shouldn’t be, it’s still very efficient. But based on your comment that you want to do in-place editing, you can still do that with distinct commands (sed commands rather than invocations of sed itself). You can either use multiple … Read more

Printing verbose progress from sed and awk

This is based on potong’s answer. The following code replaces ‘ll’ with ‘zz’, creates a backup file, displays the new text, and writes the change(s) into the file. $ echo hello > test $ sed -e ‘s/ll/zz/;w /dev/stdout’ -i .backup test hezzo $ cat test hezzo $ cat test.backup hello

Escape single quote

Your question is a little confusing since there’s no me in the original string to replace. However, I think I have it. Let me paraphrase: I have a sed command which can successfully replace a single quote ‘ with the word me. I want a similar one which can replace it with the character sequence … Read more

sed: insert a string after every N lines

This thread is another example of how to over complicate things. This should do it: sed ‘0~30 s/$/string/g’ < inputfile > outputfile Every 30 lines “string” is inserted at the end of the line. If you want a new line with the word “string” just use “\n string”.

BSD sed: extra characters at the end of d command

For anyone who found this question due to a similar error message, but caused by an attempt to in-place edit on Mac OS X As per https://github.com/lmquang/til/issues/18: OS X requires the extension to be explicitly specified. The workaround is to set an empty string: $ sed -i ” ‘s/megatron/pony/g’ /path/to/file.txt ^^ man sed: -i extension … Read more

Delete using a different delimiter with Sed

The delimiters // that you’re using are not for the d command, they’re for the addressing. I think you’re comparing them to the slashes in the s/// command… However although both relate to regular expressions, they are different contexts. The address (which appears before the command) filters which lines the command is applied to… The … Read more

What does \1 in sed do?

Here’s a simple example: $ echo ‘abcabcabc’ | sed ‘s/\(ab\)c/\1/’ ababcabc $ echo ‘abcabcabc’ | sed ‘s/\(ab\)c/\1/g’ ababab $ echo ‘abcabcabc’ | sed ‘s/\(ab\)\(c\)/\1d\2/g’ abdcabdcabdc In the first command, only the first match is affected. In the second command, every match is affected. In both cases, the \1 refers to the characters captured by the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)