Sed: Difference between revisions
m Text replacement - "Category:Linux{{Navbox Linux}}" to "{{Navbox Linux}}Category:Linux" |
m formatting |
||
| Line 4: | Line 4: | ||
Using the above tips, you can modify a file directly by using the {{code|-i}} parameter, with the filename as the second argument: | Using the above tips, you can modify a file directly by using the {{code|-i}} parameter, with the filename as the second argument: | ||
{{highlight|lang=terminal|code= | |||
$ sed -i "search" /path/to/file | |||
}} | |||
== Simple search / replace == | == Simple search / replace == | ||
Global search and replace: | Global search and replace: | ||
{{highlight|lang=terminal|code= | |||
$ sed 's/<search>/<replace>/g' | |||
}} | |||
To replace only the first match: | To replace only the first match: | ||
{{highlight|lang=terminal|code= | |||
$ sed '0,/<search>/s//<replace>/' | |||
}} | |||
== Remove HTML tags == | == Remove HTML tags == | ||
{{highlight|lang=terminal|code= | |||
$ sed -e 's/<[^>]*>//g' <file> | |||
}} | |||
== Remove File Extensions == | == Remove File Extensions == | ||
{{highlight|lang=terminal|code= | |||
$ sed 's/\(.*\)\..*/\1/' | |||
}} | |||
eg: | eg: | ||
{{highlight|lang=terminal|code= | |||
$ echo "house.txt" {{!}} sed 's/\(.*\)\..*/\1/' # returns 'house' | |||
}} | |||
== Delete Matching Line == | == Delete Matching Line == | ||
{{highlight|lang=terminal|code= | |||
$ sed '/someline/d' | |||
}} | |||
== Inserting a Line Before/After a Match == | == Inserting a Line Before/After a Match == | ||
Use: {{code|0,/<regex>/s//<replacement text where \0 is the found line>/}} | Use: {{code|0,/<regex>/s//<replacement text where \0 is the found line>/}} | ||
{{highlight|lang=terminal|code= | |||
## Add a password line before the first title entry in the grub.conf file. | |||
$ sed -i "0,/^title/s//password --encrypted $Password\n\n\0/" $GrubConfig | |||
}} | |||
To append after, just put \0 before your replacement text. | To append after, just put \0 before your replacement text. | ||
| Line 57: | Line 73: | ||
== Grap Specific Section using Regex == | == Grap Specific Section using Regex == | ||
To grab a specific section of a string, for instance '3' in 'Server time offset: 3': | To grab a specific section of a string, for instance '3' in 'Server time offset: 3': | ||
{{highlight|lang=terminal|code= | |||
$ echo 'Server time offset: 3' {{!}} sed -n -e 's/.*: \([0-9]*\)/\1/p' | |||
}} | |||
This can also be done with grep, though it isn't regex: | This can also be done with grep, though it isn't regex: | ||
{{highlight|lang=terminal|code= | |||
$ echo 'Server time offset: 3' {{!}} /bin/grep -ohE '[0-9]*' | |||
}} | |||
== Join Every Other Line == | == Join Every Other Line == | ||
| Line 67: | Line 88: | ||
'$!N;s/\n/ /' | '$!N;s/\n/ /' | ||
}} | }} | ||
== Further Reading == | == Further Reading == | ||
Revision as of 03:03, 12 September 2019
Modifying a File Directly
Using the above tips, you can modify a file directly by using the -i parameter, with the filename as the second argument:
$ sed -i "search" /path/to/file
Simple search / replace
Global search and replace:
$ sed 's/<search>/<replace>/g'
To replace only the first match:
$ sed '0,/<search>/s//<replace>/'
Remove HTML tags
$ sed -e 's/<[^>]*>//g' <file>
Remove File Extensions
$ sed 's/\(.*\)\..*/\1/'
eg:
$ echo "house.txt" | sed 's/\(.*\)\..*/\1/' # returns 'house'
Delete Matching Line
$ sed '/someline/d'
Inserting a Line Before/After a Match
Use: 0,/<regex>/s//<replacement text where \0 is the found line>/
## Add a password line before the first title entry in the grub.conf file.
$ sed -i "0,/^title/s//password --encrypted $Password\n\n\0/" $GrubConfig
To append after, just put \0 before your replacement text.
Another example: To add After=network-online.target in a systemd service file:
...
# lightdm takes responsibility for stopping plymouth, so if it fails
# for any reason, make sure plymouth still stops
OnFailure=plymouth-quit.service
[Service]
ExecStart=/usr/sbin/lightdm
Restart=always
...
Running this will append an 'After' dependency to the service file before the service section.
terminal
# sed -i "0,/^\[Service\]/s//After=network-online.target\n\n\0/" /usr/lib/systemd/system/lightdm.service
Grap Specific Section using Regex
To grab a specific section of a string, for instance '3' in 'Server time offset: 3':
$ echo 'Server time offset: 3' | sed -n -e 's/.*: \([0-9]*\)/\1/p'
This can also be done with grep, though it isn't regex:
$ echo 'Server time offset: 3' | /bin/grep -ohE '[0-9]*'
Join Every Other Line
To join every other line, useful for making key:value pairs, use:
'$!N;s/\n/ /'