Sed: Difference between revisions
Jump to navigation
Jump to search
Anthoanthop (talk | contribs) No edit summary |
Anthoanthop (talk | contribs) No edit summary |
||
(27 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Print a pattern between two (exclusive) words= | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
echo "pattern" | sed -e 's/.*WORD1//' -e 's/WORD2.*$//' | echo "pattern" | sed -e 's/.*WORD1//' -e 's/WORD2.*$//' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Example: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 10: | Line 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If we only want the <i>routedTo</i> information: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ ./ovh-api-bash-client.sh --url "/ip/45.102.38.141" | sed -e 's/.*serviceName":"//' -e 's/"},"ip":.*$//' | $ ./ovh-api-bash-client.sh --url "/ip/45.102.38.141" | sed -e 's/.*serviceName":"//' -e 's/"},"ip":.*$//' | ||
ns612429.ip-45-102-38.eu | ns612429.ip-45-102-38.eu | ||
</syntaxhighlight> | |||
=Delete pattern between two words= | |||
<syntaxhighlight lang="bash"> | |||
sed '/WORD1/,/WORD2/d' input | |||
</syntaxhighlight> | |||
=Replace pattern between two words= | |||
<syntaxhighlight lang="bash"> | |||
sed '/\[ssh-ddos\]/,/filter = sshd-ddos/s/enabled = false/enabled = true/' /etc/fail2ban/jail.conf | |||
</syntaxhighlight> | |||
=Extract from specific line to EOF= | |||
<syntaxhighlight lang="bash"> | |||
sed -n '44519778,$p' input.log > output.log | |||
</syntaxhighlight> | |||
=Replace new lines with a comma and a space= | |||
<syntaxhighlight lang="bash"> | |||
sed -z 's/\n/, /'g my_file | |||
</syntaxhighlight> | |||
=Remove all leading blank lines at top of a file= | |||
<syntaxhighlight lang="bash"> | |||
sed '/./,$!d' | |||
</syntaxhighlight> | |||
=Comment out a file from line 25 to end of file= | |||
<syntaxhighlight lang="bash"> | |||
sed '25,$s/^/#/' | |||
</syntaxhighlight> | |||
=To append a text after a specific pattern= | |||
<syntaxhighlight lang="bash"> | |||
sed '/adImpressions=1/a bid_response_http_error_body=0' | |||
</syntaxhighlight> | |||
=Insert line after first matching pattern= | |||
<syntaxhighlight lang="bash"> | |||
sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' input | |||
</syntaxhighlight> | |||
=Insert line before matching patterns= | |||
<syntaxhighlight lang="bash"> | |||
sed -i '/\/var\/log\/syslog/i \:msg, contains, \"Successfully sent\" ~' /etc/rsyslog.conf | |||
</syntaxhighlight> | |||
Will insert: | |||
:msg, contains, "Successfully sent" ~ | |||
before: | |||
/var/log/syslog | |||
=Delete lines between PATTERN-1 and PATTERN-2, excluding the lines containing these patterns= | |||
<syntaxhighlight lang="bash"> | |||
sed '/PATTERN-1/,/PATTERN-2/{//!d}' my_input.log | |||
</syntaxhighlight> | |||
=Insert the content of a file after a matching pattern= | |||
<syntaxhighlight lang="bash"> | |||
sed '/\[mongo\]/r /root/scripts/input_file' file_to_edit | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 15:32, 19 June 2018
Print a pattern between two (exclusive) words
echo "pattern" | sed -e 's/.*WORD1//' -e 's/WORD2.*$//'
Example:
$ ./ovh-api-bash-client.sh --url "/ip/45.102.38.141"
200 {"organisationId":null,"country":"fr","routedTo":{"serviceName":"ns612429.ip-45-102-38.eu"},"ip":"45.102.38.141/32","canBeTerminated":true,"type":"failover","description":null}
If we only want the routedTo information:
$ ./ovh-api-bash-client.sh --url "/ip/45.102.38.141" | sed -e 's/.*serviceName":"//' -e 's/"},"ip":.*$//'
ns612429.ip-45-102-38.eu
Delete pattern between two words
sed '/WORD1/,/WORD2/d' input
Replace pattern between two words
sed '/\[ssh-ddos\]/,/filter = sshd-ddos/s/enabled = false/enabled = true/' /etc/fail2ban/jail.conf
Extract from specific line to EOF
sed -n '44519778,$p' input.log > output.log
Replace new lines with a comma and a space
sed -z 's/\n/, /'g my_file
Remove all leading blank lines at top of a file
sed '/./,$!d'
Comment out a file from line 25 to end of file
sed '25,$s/^/#/'
To append a text after a specific pattern
sed '/adImpressions=1/a bid_response_http_error_body=0'
Insert line after first matching pattern
sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' input
Insert line before matching patterns
sed -i '/\/var\/log\/syslog/i \:msg, contains, \"Successfully sent\" ~' /etc/rsyslog.conf
Will insert:
:msg, contains, "Successfully sent" ~
before:
/var/log/syslog
Delete lines between PATTERN-1 and PATTERN-2, excluding the lines containing these patterns
sed '/PATTERN-1/,/PATTERN-2/{//!d}' my_input.log
Insert the content of a file after a matching pattern
sed '/\[mongo\]/r /root/scripts/input_file' file_to_edit