r/sed • u/Scrabbilisk • Jul 01 '19
r/sed • u/grischnack • Mar 26 '21
I created the most complicated sed script in the world
Please have a look at my repo at https://github.com/grischnack/springy-sed-typer
It is basically a shell that can execute commands and you can scroll through the stdout by echo-ing letters into a fifo. It has many bugs but it shows the principle of achieving complex behaviour using simple tools. Also parallel processing is done using sed (with the 'e' option of the switch command).
If you have question on how to run it, you can ask here.
r/sed • u/frisbeethecat • Oct 09 '25
sed made it easy
I had to search and replace "$" for "/$" for several hundred .tex files. A short bash script and sed -i 's/\$/\\$/g' "$filename" and it's done. I'm not really a *nix person, and it took me a while to figure out the proper syntax and all, but that was far better than trudging through the directory manually. So, thanks, sed!
Is there a way to make self-starting sed scripts?
For interactive/fun sed scripts, a recurring problem I have is making them "start" without having to input at least one line. For example in my text RPG in sed, you get nothing when you just start the script until you press enter. This is a bit ugly and not very user friendly.
So is there any way to just get some text printed or even run code before having the first line of input? (GNU-specific solutions are also fine.)
r/sed • u/electricalkitten • Jan 22 '24
Add string after last pattern match
Hi everybody,
I have to add 'Cipher a,b,c' to an sshd_config file on the next line after the last HostKey.* pattern. I must not append 'Cipher a,b,c' to the end of the file because it breaks any ssh Match blocks that I have on some of the server sshd_config files.
So what can I do? I tried this, and got close, but it just appended it at the end again instead of after the HostKey.* strings
-z 's/^HostKey.*/&a\Ciphers a,b,c/'Here is an example of the intitial sshd_config file:
HostKey /etc/ssh/ssh_host_tinykey
HostKey /etc/ssh/ssh_host_bigkey
HostKey /etc/ssh/ssh_host_fookey
AllowUsers cat,dog,dino
Port 22I should like to get to this state in the file:
HostKey /etc/ssh/ssh_host_tinykey
HostKey /etc/ssh/ssh_host_bigkey
HostKey /etc/ssh/ssh_host_fookey
Ciphers a,b,c
AllowUsers cat,dog,dino
Port 22I can find the last occurance of the HostKey string in the file with this, but it's not exactly what I wanted.
-n '/string/h;${x;p;}' I admit I am out of my depth.
Does anybody know how to do this?
Many thanks, EK
r/sed • u/deuvisfaecibusque • Feb 25 '23
Help me understand why/how this works (multi-line handling)
The idea came to me to use ffmpeg to split audio files based on start and end timecodes — useful for albums sourced from Youtube, for example.
So the raw timecodes look like this:
00:00 Track 1
03:28 Track 2
05:34 Track 3and I want to get this:
00:00 03:28 Track 1
03:28 05:34 Track 2
05:34 09:54 Track 3I have found that using the following works, but I don't understand why:
sed -E -e 'N;/^([0-9:]+) (.+)\n([0-9:]+) (.+)/p;D' sample_timecodes_tester.txt | sed -E 'N;s/^([0-9:]+) (.+)\n([0-9:]+) (.+)/\1\t\3\t\2\n\3\t\4/g' | sed -n 'p;n'The first sed duplicates all but the first and last lines.
The second sed produces the expected output, but only on odd-numbered lines — why?
The third sed prints only the odd-numbered lines.
This also feels rather clunky — is there no way to do it by calling sed just once?
breaking up a long wordlike string
I have a data set that ends up looking like this:
aaaabbbeeffffjjjzz
or something similar. It ends up being a long string of lowercase letters in alphabetical order. I want to transform it into this:
aaaa
bbb
ee
ffff
jjj
zzCurrently I am using:
sed -E "s/(a)([^a])/\1\n\2/;s/(b)([^b])/\1\n\2/; ... s/(y)([^y])/1\n\2/"
which works, but is long and inelegant. I have tried:
sed -E "s/(.)([^\1])/\1\n\2/g"
Which sort of works, but breaks everything into groups of two. I don't quite follow why.
I am looking for some generalized regular expression that finds the "borders" between groups of letters. For instance, it would catch a single character followed by another single character that isn't that single character.
r/sed • u/letITsknow • Nov 29 '21
Having trouble with sed substitution
So i'd like sed to give me the version number, which looks like this: 11.111.111, of a software from their website.
This is the command i tried (in various versions):
curl --silent https://website.whatever.com/new_version | sed 's/.*\([0-9]\+.[0-9]\+.[0-9]\+\).*/\1/'
This is the line of the source code in which the version number is located:
v. 10.135.236 | ZIP Format | 25.7 Mb<br/>
When i do this the output is always the entire source code of the website. Am i missing something? I'm pretty new to sed and regex in general, so an explanation would be appreciated a lot.
Thanks in advance!
r/sed • u/exquisitesunshine • Oct 03 '21
[noob] Why doesn't this work for trimming out file extension?
s!\.*$!! doesn't work but s!\.[^.]*$!! does, why? The filename may or may not have an extension (e.g. .sh).
Also, suppose I have ext=.sh. How can I then modify the above to replace "the last dot to the end of the line" with $ext? I must have got the quotes wrong or didn't escape something properly.
r/sed • u/PacoVelobs • Jan 05 '23
Check if capture group exists
Hello there :wave:
I've this sed 's/.*{\([^}]\+\)}\(\[\([^]]\+\)\]\)\?$/\1 :: \3/g' line.
I use it to parse neorg files and find links in a much longer command :
xdotool type $(find ~/.neorg/ -name '*.norg' -exec cat {} \; | grep --extended-regexp '{[^}]+}(\[[^]]*\])?' | sed 's/.*{\([^}]\+\)}\(\[\([^]]\+\)\]\)\?$/\1 :: \3/g' | rofi -i -dmenu | cut -d ' ' -f 1)It's heavily inspired by https://www.youtube.com/watch?v=d_11QaTlf1I. Also, it may look overcomplicated but I need to handle cases where there are multiple links per line (hence the non-greedy hacky thing).
Anyway, I'd like to know if there is a way to avoid showing the :: bit if \3 does not exist.
Googling or DuckDuckGoing did not bring any solution for now. Maybe I am asking the wrong question.
Many thanks in advance :pray:
P
r/sed • u/yogibjorn • Jun 21 '22
How do I use Sed to create a space after a period stop.
How do I use Sed to create a space after a period stop. For example: This is a test.This is a test Would become: This is a test. This is a test.
r/sed • u/ehoffm01 • Mar 26 '22
Pipe delimited file with a pipe in text string
I need to replace the pipe in the following pipe delimited line with a dash
abc|12345|”efg | hij”|k678l
So the resulting line looks like
abc|12345|”efg - hij”|k678l
I can find the line but haven’t been able to replace the pipe in the string “efg | hij”. Thanks for any help you all can provide.
This command worked for me.
<code> sed 's/^\([^"]*\)"\([^|]*\)|\([^"]*\)"/\1"\2-\3"/g' <\code>
r/sed • u/WithAnAitchDammit • Mar 08 '22
sed to add two forward slashes to the beginning of a line in a file
I'm writing a bash script to make some changes to a PHP config file. I'm having a hard time understanding exactly how to escape out the single quotes while maintaining the literal variable.
I know I can't use a double quote (") because that would expand the variable to its value, which I don't want.
My test PHP file is:
<?php
$cfg['TempDir'] = '/tmp';
?>I want the line to be:
//$cfg['TempDir'] = '/tmp';The sed command in my script I have tried is:
sed -i 's|$cfg['"'"'Temp|\/\/$cfg['"'"'Temp|g' test.phpwhich results in:
sed: -e expression #1, char 29: unterminated `s' commandAnother attempt is to escape out the (') instead of quoting it out:
sed -i 's|$cfg['\''Temp|\/\/$cfg['\''Temp|g' test.phpWhich results in the same error.
Any help is appreciated!
r/sed • u/[deleted] • Sep 19 '21
Need help to modify #include directive in lot's of .h files
Hi everybody,
I need to modify lot's of include directive in .c/.h files.
I tried lot's of thing to do this.
When I try sed -n '/#include/p' $(find -type f -name "*.h"), I can list all include in my all .h files.
I need to change the original path(plist/plist.h in ../../libplist/include/plist) of include with sed 's#plist#../../libplist/include/plist/#'
I tried to do with one command sed -n "/#include/p" -e 's#plist#../../libplist/include/plist/#' $(find ....)
but that prints sed: #include/p no such file or directory.
I tried with ; and { } to avoid -n and -e command but that doesn't works
How can I do to solve this problem ?
Thank's in advance
r/sed • u/Patrona_ • Aug 16 '21
Why doesn't the 1d command execute? And why doesn't the output stop at the first occurrence of </p>?
r/sed • u/immortal192 • Jul 24 '21
Convert sed command in single quotes to double quotes and use variable
I have a working sed command (through massive trial and error): sed -z 's/\n/\\\\n/g; s/\(.*\)\\\\n/\1/; s/\(.*\)\\\\n/\1\\\\n<i>/; s/$/<\/i>/', which replaces \n with \\n in the first sub command, removes the last instance of \\n in the second, replaces the (new) last instance of the \\n with <i> in the third, and sends the text with </i> in the fourth sub command.
In the third sub command s/\(.*\)\\\\n/\1\\\\n<i>/, how can I convert this command to instead replace the nth last instance of \\n with <i> (i.e. 7thth last \\n with <i>) instead of the last instance?
Simply replacing the entire sed command's single quotes with double quotes with the intention of using a variable like s/\(.*\)\\\\n/\"$var"\\\\n<i>/ list.txt doesn't yield in expected results. Here's what it's supposed to look like with the input text list.txt:
Correct output: chrono-date 3.0.0-2 -> 3.0.1-1\\nlibsidplayfp 2.2.0-1 -> 2.2.1-1\\nlinux 5.12.15.arch1-1 -> 5.13.4.arch1-1\\nlinux-headers 5.12.15.arch1-1 -> 5.13.4.arch1-1\\nredshift-scheduler 1.2.0-1 -> 1.3.0-1\\nvulkan-icd-loader 1.2.184-1 -> 1.2.185-1\\nwaybar 0.9.7-4 -> 0.9.7-5\\nwebkit2gtk 2.32.2-1 -> 2.32.3-1\\nchrono-date 3.0.0-2 -> 3.0.1-1\\nlibsidplayfp 2.2.0-1 -> 2.2.1-1\\nlinux 5.12.15.arch1-1 -> 5.13.4.arch1-1\\nlinux-headers 5.12.15.arch1-1 -> 5.13.4.arch1-1\\nredshift-scheduler 1.2.0-1 -> 1.3.0-1\\nvulkan-icd-loader 1.2.184-1 -> 1.2.185-1\\nwaybar 0.9.7-4 -> 0.9.7-5\\n<i>webkit2gtk 2.32.2-1 -> 2.32.3-1</i>
Simply replacing single with double quotes first:
chrono-date 3.0.0-2 -> 3.0.1-1\
libsidplayfp 2.2.0-1 -> 2.2.1-1\
linux 5.12.15.arch1-1 -> 5.13.4.arch1-1\
linux-headers 5.12.15.arch1-1 -> 5.13.4.arch1-1\
redshift-scheduler 1.2.0-1 -> 1.3.0-1\
vulkan-icd-loader 1.2.184-1 -> 1.2.185-1\
waybar 0.9.7-4 -> 0.9.7-5\
webkit2gtk 2.32.2-1 -> 2.32.3-1\
chrono-date 3.0.0-2 -> 3.0.1-1\
libsidplayfp 2.2.0-1 -> 2.2.1-1\
linux 5.12.15.arch1-1 -> 5.13.4.arch1-1\
linux-headers 5.12.15.arch1-1 -> 5.13.4.arch1-1\
redshift-scheduler 1.2.0-1 -> 1.3.0-1\
vulkan-icd-loader 1.2.184-1 -> 1.2.185-1\
waybar 0.9.7-4 -> 0.9.7-5\
webkit2gtk 2.32.2-1 -> 2.32.3-1\Any ideas on how to use a variable and/or how to simplify the sed command is much appreciated.
Further context: This is for a custom Waybar module where I want the module to show a list of new package updates and italicize the N packages at the end that are from the AUR (I use Arch Linux). The output should be in the PANGO markup language in a single line.
r/sed • u/B1GF31N • Apr 23 '21
sed and spaces
So I'm trying to write a bash script to install and configure Monit. I need to configure the monitrc file and one of the lines I'm trying to edit is set daemon 30 to be set daemon 60 whatever I try it either doesn't do anything or just makes the entire file blank. This is what I have been trying: sed -e "s/set\sdaemeon\s30/set\sdaemeon\s60/" /etc/monitrc > /etc/monitrc
edit: forgot e in daemon and misspelled it
edit 2: snippet of monitrc:
###############################################################################
## Global section
###############################################################################
##
## Start Monit in the background (run as a daemon):
#
set daemon 30 # check services at 30 seconds intervals
# with start delay 240 # optional: delay the first check by 4-minutes (by
# # default Monit check immediately after Monit start)
#
#
## Set syslog logging. If you want to log to a standalone log file instead,
## specify the full path to the log file
#
r/sed • u/GermanPCBHacker • Jan 19 '26
Join multiple lines by a number?
I am curious. Is it possible to concatenate a stream of lines with sed only by a number?
Assume that input:
echo 'blah-1-n-1-blah
blah-2-n-1-blah
blah-2-n-1-blah
blah-2-n-2-blah
blah-3-n-1-blah' | sed magicI want sed to concat the lines based on the "-n-([0-9]+)-" pattern. Expected result:
blah-1-n-1-blah blah-2-n-1-blah
blah-1-n-2-blah blah-2-n-2-blah
blah-1-n-3-blahBefore anyone says: That can be done simpler... I know there are basically infinite ways. But I want to know if sed can do that. I mean concatenation of 2 lines into 1 is easy. But unknown number of lines by a group marker like a number in this case... Is that possible? I cannot get this thought out of my head and I need to know it to sleep again!
r/sed • u/ajm11111 • Nov 09 '24
Replacing 0x0A - can replace other characters, except 0x0A
Trying to replace 0x0A character in string input, with null. I can replace all other characters except 0x0A... what am I missing here?
6e 65 72 69 63 20 36 2e 38 2e 31 32 29 0a 4e 6f (Original input)
sed 's/\x4e/\x00/g'6e 65 72 69 63 20 36 2e 38 2e 31 32 29 0a 00 6f (expected 0x4E replaced with 0x00)
sed 's/\x0a/\x00/g'6e 65 72 69 63 20 36 2e 38 2e 31 32 29 0a 4e 6f (unexpected, 0x0A not replaced with 0x00)
r/sed • u/doctorzeus1aonly • May 25 '23
Replacing MSSQL DateTime Format With Postgres Timestamp using Sed
So I have been moving some database table data from MSSQL to PostGres and have exported the data as CSV files ready to be re-imported but are having trouble trying to sanitize the datetime format to timestamp using sed (all dates and times are UTC).
As far as I can see there are only a few differences (namely the 'T', '.' being replaced with ':' and the end 'Z)
DATETIME: 2023-05-25 03:36:02.070
TIMESTAMP: 2023-05-25T03:36:92.981Z
What is the best way to go about replacing the space with the T, '.' with ':' etc?
Many Thanks
r/sed • u/OuiMaisMoiChristophe • Mar 18 '23
Execute sed command remotely using remote env variable
I need to run:
john@pc ~$ ssh [email protected] sudo -- "sed -i 's/replace/$USER/g' /root/test.txt" Here $USER is parsed as a local env variable. It will be "john" on my machine.
I want $USER to be parsed as a remote env variable: the word "replace" should be replaced by "root" as root is the user we use in our ssh connection ($USER=root on the remote machine).
I know the below example will give us "john":
john@pc ~$ ssh [email protected] sudo -- "echo $USER" The below one will give us "root":
john@pc ~$ ssh [email protected] sudo -- "echo \$USER" Then I tried:
john@pc ~$ ssh [email protected] sudo -- "sed -i 's/replace/\$USER/g' /root/test.txt" but nothing works. I also tried 's/replace/\\$USER/g', 's/replace/$$USER/g' and many others but I can't find the proper solution.
r/sed • u/EmilySeville7cfg • Jan 31 '23
Print just first lines beginning with "> " via sed doesn't work
Hi! Here is my input file:
# [
> Check file types and compare values
> Returns 0 if the condition evaluates to true, 1 if it evaluates to false
> More information: https://www.gnu.org/software/bash/manual/bash.html#index-test
- Test if a given variable [is] equal/not equ[a]l to the specified string:
`[ "${string value: variable}" {string value: ==|!=} "{string value: string}" ]`Here is my script:
#!/usr/bin/env bash
declare file="/home/emilyseville7cfg/Documents/mine/other/cli-pages/common/[.btldr"
sed -nE '/^>/ {
h
:description
N
H
/> (Aliases|See also|More information):/! bdescription
s/^.*\n//
x
p
Q
}' "$file"I wanna print just all consecutive lines beginning with > but not having (Aliases|See also|More information): at the beginning. I expect such lines to be in the pattern buffer at the end of the script but surprisingly I get this output:
> Check file types and compare values
> Check file types and compare values
> Returns 0 if the condition evaluates to true, 1 if it evaluates to false
> Check file types and compare values
> Returns 0 if the condition evaluates to true, 1 if it evaluates to false
> More information: https://www.gnu.org/software/bash/manual/bash.html#index-testNote that hold space should contain > More information: https://www.gnu.org/software/bash/manual/bash.html#index-test at the end of the script. In other words, the first line with (Aliases|See also|More information): at the beginning.
My idea was to condense all lines starting with an angle bracket in the pattern space excluding the last one containing (Aliases|See also|More information):. What am I missing to implement what I want? I need join several lines as I wanna colorize them at once and surround with two arbitrary strings (which user wants) to have smth like this at the end:
# [
[ Check file types and compare values
Returns 0 if the condition evaluates to true, 1 if it evaluates to false ]
> More information: https://www.gnu.org/software/bash/manual/bash.html#index-test
- Test if a given variable [is] equal/not equ[a]l to the specified string:where [ and ] are two above mentioned strings.