sed editing a line - sed

I'm trying to use sed to modify a line like below
CustomLog /home/logs/httpd/access_log common
to
CustomLog "|/usr/sbin/rotatelogs /home/logs/httpd/access_log_%m_%d_%Y 86400" combined
I tried sed replace with text and it didn't work. I'm trying to do this for around 800 files, so need a single command to do this.

This seems to work:
sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\)/\1 "|\/usr\/sbin\/rotatelogs \2_`date +%m_%d_%Y` 86400" combined/'
Example:
> echo "CustomLog /home/logs/httpd/access_log common" | sed 's/\([^ ]*\) \([^ ]*\) \([^ ]*\)/\1 "|\/usr\/sbin\/rotatelogs \2_`date +%m_%d_%Y` 86400" combined/'

sed -i.bak '/CustomLog/{
s# # "|/usr/sbin/rotatelogs #
s/ common$/_%m_%d_%Y 86400" combined/
}' *.conf

Related

Removing repeated characters with sed command

How to remove repeated characters or symbols in a string
some text\n\n\n some other text\n\n more text\n
How can I make something like this using sed or another command?
some text\n some other text\n more text\n
I can remove \n like sed s/\n//g but this will remove all the characters.
You can use
sed '/^$/d' file > newfile
In GNU sed, you can use inline replacement with -i option:
sed -i '/^$/d' file
In MacOS, FreeBSD sed inline replacement can be done with
sed -i '' '/^$/d' file
sed -i.bak '/^$/d' file
See the online demo:
#!/bin/bash
s=$(echo -e "some text\n\n\n some other text\n\n more text\n")
sed '/^$/d' <<< "$s"
Output:
some text
some other text
more text
You can also use tr if it supports squeezings.
$ echo -e 'ab\n\ncd' | tr --squeeze-repeats '\n'
ab
cd
Given the following [input] or a file that is similar:
printf "some text\n\n\n some other text\n\n more text\n" | [ one of the pipes below... ]
Any of these work:
[input] | sed -n '/[^[:space:]]/p'
Or:
[input] | sed '/^$/d'
Or, if you want to filter ^[spaces or tabs]\n also:
[input] | sed '/^[[:blank:]]*$/d'
Or with awk:
[input] | awk 'NF'

sed delete also part of IP address

can please somebody help me with this?
I have this line
test.txt
siemplog1.nw.lan / 172.31.180.22
I tried this command sed -Ei "s/^[a-z A-Z].*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/" test.txt
result should be 172.31.180.22 but I got this 2.31.180.22
thank you
The .* matches as many chars as it can (it is "greedy") and since [0-9]{1,3} can match just 1 digit, the 17 is matched by the .* and 2 is matched by [0-9]{1,3}.
You may stop the .* before any non-digit:
sed -Ei 's~.*[^0-9]([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*~\1~' test.txt
Or, before /:
sed -Ei 's~.*/ *([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*~\1~' test.txt
See online sed demo:
s='siemplog1.nw.lan / 172.31.180.22'
sed -E 's~.*/ *([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*~\1~' <<< "$s"
# => 172.31.180.22
If you string is always in this format, you might simplify the sed command to
sed -E 's~.*/ *([0-9.]+)~\1~p'
sed -E 's~.*/ *([0-9.]+).*~\1~p'
If you have space before ip
$echo siemplog1.nw.lan / 172.31.180.22 | sed -E "s/.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/"
172.31.180.22
$

Replacing the test with sed

I'm trying to replace the text using the sed, but it's showing some error. Not getting where I'm getting wrong.
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/\callback/g' input.txt
Have this :
process.env.REDIRECT_URI
Replace this with :
http://test-domain.apps.io
Try:
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Notes:
The original command has a spurious string /\callback. All that was needed to make the code work was to remove it.
. is a wildcard. If you want to be sure that you are matching periods, they should be escaped:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Sometimes, its clearer if one doesn't have to escape /. One can use a separator of one's choice. For example, use #:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io#g' input.txt
If you did want /callback in the output, use:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io\/callback/g' input.txt
or:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io/callback#g' input.txt

Search replace regular expression variable using sed

This is probably a trivial one:
I have a file (my.file) with these lines:
>h1_c1
>h1_c2
>h1_c3
>h2_c1
>h2_c2
>h2_c3
and I want to change it in place to be:
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h3
I thought this ought to do it:
sed -i 's/\(\>\)\(h1\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file
sed -i 's/\(\>\)\(h2\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file
but it doesn't seem to work. How do I do it?
The obvious sed for your example is:
$ sed -i~ -e 's/^>\(h[0-9]\)_\(c[0-9]\)/>\2_\1/' *.foo
I tested this and it works for your example file.
Try this awk
awk -F">|_" '{print ">"$3"_"$2}' my.file > tmp && mv tmp my.file
awk -F">|_" '{print ">"$3"_"$2}' my.file
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2
You can try this sed,
sed 's/>\(h[1-2]\)_\(.*\)/>\2_\1/' yourfile
(OR)
sed -r 's/>(h[1-2])_(.*)/>\2_\1/' yourfile
kent$ sed -r 's/>([^_]*)_(.*)/>\2_\1/' f
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2
you add -i if you want it to happen "in-place"

sed - substitute either of two characters with one command

I would like one sed command to accomplish the following:
$ sed s'/:/ /g' <and> sed s'/=/ /g'
That is, I would like to write
sed s'/<something>/ /g'
and have both = and : replaced by space.
sed s'/[:=]/ /g'
Brackets mean "any one of".
One option is also to use sed -e, like this. Although you don't need it in this case, it's however a good option to know about.
sed -e 's/:/ /' -e 's/..../ /' file
Sanjay's answer solves it. Another option that works with only one sed command is to separate each s substitution with a semicolon
sed 's/:/ /g ; s/=/ /g' file
or in separate lines in a script
sed 's/:/ /g
s/=/ /g' file
Those may be handy in other situations.