r/sed • • Aug 03 '21

if you know you know 😂

Post image
3 Upvotes

r/sed • • Dec 15 '19

I need to change "PasswordAuthentication" from yes to no in /etc/ssh/sshd_config, but nothing happens.

1 Upvotes

I am using the following on the commandline:

sed 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

Any suggestions as to why its not working?


r/sed • • Sep 15 '21

Sed Regex Help!!!

1 Upvotes

i have the following sed regex but needs the output to be in alphabetical order as well.

sed 's/^abc\///'

can someone help please?


r/sed • • Jan 05 '21

What does the * in the expression, weren't it will works

Thumbnail
ibb.co
0 Upvotes

r/sed • • Apr 22 '21

Sed search and replace for chemistry Symbols in latex, MacOS help.

1 Upvotes

Hi, i have the following script taken from here

I have only removed the ^ and $ and replaced it with [[:<:]] and [[:>:]] as appropriate for macos. regex101 shows me the capture group i want is match no 124.

my script as i am trying to use is ( code was autogenerated on regex101 i modified it very slightly.)

sed -E "s#(?(DEFINE)
  (?# Periodic elements )
  (?<Hydrogen>H)
  (?<Helium>He)
  (?<Lithium>Li)
  (?<Beryllium>Be)
  (?<Boron>B)
  (?<Carbon>C)
  (?<Nitrogen>N)
  (?<Oxygen>O)
  (?<Fluorine>F)
  (?<Neon>Ne)
  (?<Sodium>Na)
  (?<Magnesium>Mg)
  (?<Aluminum>Al)
  (?<Silicon>Si)
  (?<Phosphorus>P)
  (?<Sulfur>S)
  (?<Chlorine>Cl)
  (?<Argon>Ar)
  (?<Potassium>K)
  (?<Calcium>Ca)
  (?<Scandium>Sc)
  (?<Titanium>Ti)
  (?<Vanadium>V)
  (?<Chromium>Cr)
  (?<Manganese>Mn)
  (?<Iron>Fe)
  (?<Cobalt>Co)
  (?<Nickel>Ni)
  (?<Copper>Cu)
  (?<Zinc>Zn)
  (?<Gallium>Ga)
  (?<Germanium>Ge)
  (?<Arsenic>As)
  (?<Selenium>Se)
  (?<Bromine>Br)
  (?<Krypton>Kr)
  (?<Rubidium>Rb)
  (?<Strontium>Sr)
  (?<Yttrium>Y)
  (?<Zirconium>Zr)
  (?<Niobium>Nb)
  (?<Molybdenum>Mo)
  (?<Technetium>Tc)
  (?<Ruthenium>Ru)
  (?<Rhodium>Rh)
  (?<Palladium>Pd)
  (?<Silver>Ag)
  (?<Cadmium>Cd)
  (?<Indium>In)
  (?<Tin>Sn)
  (?<Antimony>Sb)
  (?<Tellurium>Te)
  (?<Iodine>I)
  (?<Xenon>Xe)
  (?<Cesium>Cs)
  (?<Barium>Ba)
  (?<Lanthanum>La)
  (?<Cerium>Ce)
  (?<Praseodymium>Pr)
  (?<Neodymium>Nd)
  (?<Promethium>Pm)
  (?<Samarium>Sm)
  (?<Europium>Eu)
  (?<Gadolinium>Gd)
  (?<Terbium>Tb)
  (?<Dysprosium>Dy)
  (?<Holmium>Ho)
  (?<Erbium>Er)
  (?<Thulium>Tm)
  (?<Ytterbium>Yb)
  (?<Lutetium>Lu)
  (?<Hafnium>Hf)
  (?<Tantalum>Ta)
  (?<Tungsten>W)
  (?<Rhenium>Re)
  (?<Osmium>Os)
  (?<Iridium>Ir)
  (?<Platinum>Pt)
  (?<Gold>Au)
  (?<Mercury>Hg)
  (?<Thallium>Tl)
  (?<Lead>Pb)
  (?<Bismuth>Bi)
  (?<Polonium>Po)
  (?<Astatine>At)
  (?<Radon>Rn)
  (?<Francium>Fr)
  (?<Radium>Ra)
  (?<Actinium>Ac)
  (?<Thorium>Th)
  (?<Protactinium>Pa)
  (?<Uranium>U)
  (?<Neptunium>Np)
  (?<Plutonium>Pu)
  (?<Americium>Am)
  (?<Curium>Cm)
  (?<Berkelium>Bk)
  (?<Californium>Cf)
  (?<Einsteinium>Es)
  (?<Fermium>Fm)
  (?<Mendelevium>Md)
  (?<Nobelium>No)
  (?<Lawrencium>Lr)
  (?<Rutherfordium>Rf)
  (?<Dubnium>Db)
  (?<Seaborgium>Sg)
  (?<Bohrium>Bh)
  (?<Hassium>Hs)
  (?<Meitnerium>Mt)
  (?<Darmstadtium>Ds)
  (?<Roentgenium>Rg)
  (?<Copernicium>Cn)
  (?<Nihonium>Nh)
  (?<Flerovium>Fl)
  (?<Moscovium>Mc)
  (?<Livermorium>Lv)
  (?<Tennessine>Ts)
  (?<Oganesson>Og)
  (?# Regex )
  (?<Element>(?&Actinium)|(?&Silver)|(?&Aluminum)|(?&Americium)|(?&Argon)|(?&Arsenic)|(?&Astatine)|(?&Gold)|(?&Barium)|(?&Beryllium)|(?&Bohrium)|(?&Bismuth)|(?&Berkelium)|(?&Bromine)|(?&Boron)|(?&Calcium)|(?&Cadmium)|(?&Cerium)|(?&Californium)|(?&Chlorine)|(?&Curium)|(?&Copernicium)|(?&Cobalt)|(?&Chromium)|(?&Cesium)|(?&Copper)|(?&Carbon)|(?&Dubnium)|(?&Darmstadtium)|(?&Dysprosium)|(?&Erbium)|(?&Einsteinium)|(?&Europium)|(?&Iron)|(?&Flerovium)|(?&Fermium)|(?&Francium)|(?&Fluorine)|(?&Gallium)|(?&Gadolinium)|(?&Germanium)|(?&Helium)|(?&Hafnium)|(?&Mercury)|(?&Holmium)|(?&Hassium)|(?&Hydrogen)|(?&Indium)|(?&Iridium)|(?&Iodine)|(?&Krypton)|(?&Potassium)|(?&Lanthanum)|(?&Lithium)|(?&Lawrencium)|(?&Lutetium)|(?&Livermorium)|(?&Moscovium)|(?&Mendelevium)|(?&Magnesium)|(?&Manganese)|(?&Molybdenum)|(?&Meitnerium)|(?&Sodium)|(?&Niobium)|(?&Neodymium)|(?&Neon)|(?&Nihonium)|(?&Nickel)|(?&Nobelium)|(?&Neptunium)|(?&Nitrogen)|(?&Oganesson)|(?&Osmium)|(?&Oxygen)|(?&Protactinium)|(?&Lead)|(?&Palladium)|(?&Promethium)|(?&Polonium)|(?&Praseodymium)|(?&Platinum)|(?&Plutonium)|(?&Phosphorus)|(?&Radium)|(?&Rubidium)|(?&Rhenium)|(?&Rutherfordium)|(?&Roentgenium)|(?&Rhodium)|(?&Radon)|(?&Ruthenium)|(?&Antimony)|(?&Scandium)|(?&Selenium)|(?&Seaborgium)|(?&Silicon)|(?&Samarium)|(?&Tin)|(?&Strontium)|(?&Sulfur)|(?&Tantalum)|(?&Terbium)|(?&Technetium)|(?&Tellurium)|(?&Thorium)|(?&Titanium)|(?&Thallium)|(?&Thulium)|(?&Tennessine)|(?&Uranium)|(?&Vanadium)|(?&Tungsten)|(?&Xenon)|(?&Ytterbium)|(?&Yttrium)|(?&Zirconium)|(?&Zinc))
  (?<Num>(?:[1-9]\d*)?)
  (?<ElementGroup>(?:(?&Element)(?&Num))+)
  (?<ElementParenthesesGroup>\((?&ElementGroup)+\)(?&Num))
  (?<ElementSquareBracketGroup>\[(?:(?:(?&ElementParenthesesGroup)(?:(?&ElementGroup)|(?&ElementParenthesesGroup))+)|(?:(?:(?&ElementGroup)|(?&ElementParenthesesGroup))+(?&ElementParenthesesGroup)))\](?&Num))
)
[[:<:]]((?<Brackets>(?&ElementSquareBracketGroup))|(?<Parentheses>(?&ElementParenthesesGroup))|(?<Group>(?&ElementGroup)))[[:>:]]b#\\ce\{ \124 \}#xmg;t;d" trialselfsh.tex > out.txt

This is the exact script that i am trying to use, saved as s1.sh When i run sh s1.sh

it is showing me an error of

sed: 1: "s#(?(DEFINE)
  (?# Peri ...": unterminated substitute pattern   

I do have gsed installed in my system if that would be easier for you to answer.

my input here looks like

2.	The number of formula units of calcium fluoride,  present in 146.4 g of CaF2 (the molar mass of CaF2 is 78.08 g/mol) is
(a)

(b)
(c)
(d)

3.	The total number of protons in 10 g of calcium carbonate CaCO3 is
(a)
(b)
(c)
(d)

4.	The maximum number of molecules are present in
(a)
(b)	5 L of N2 gas at STP
(c)	0.5 g of H2 gas
(d)	10 g of O2 gas

The latex output i want is putting the elements in between \ce{ symbol}

sample desired output.

   2.	The number of formula units of calcium fluoride,  present in 146.4 g of \ce{ CaF2 } (the molar mass of \ce{ CaF2 }is 78.08 g/mol) is
    (a)
    
    (b)
    (c)
    (d)
    
    3.	The total number of protons in 10 g of calcium carbonate \ce{ CaCO3 } is
    (a)
    (b)
    (c)
    (d)
    
    4.	The maximum number of molecules are present in
    (a)
    (b)	5 L of \ce{ N2 }gas at STP
    (c)	0.5 g of \ce{ H2 }gas
    (d)	10 g of \ce { O2 } gas

I know its a long post and lot of code, but i hope someone can help me out.

Thanks a lot!


r/sed • • Jan 17 '18

Help with sed

1 Upvotes

Hi, I'm trying to extract information from an HTML document, and for the most part, everything I need is encased in separate <tr></tr> tags. However, everything within those tags is separated with new lines. I was hoping there's a way to remove new lines but only within each <tr></tr> block? Currently I have:

cat paulaPerfect.html | grep "<tr>" -A28

but that's only to read the html and pipe it into grep where I can find each element through grepping for <tr> and keeping each relevant line after each <tr>

I guess essentially I have this:

<tr>
...
</tr>
<tr>
...
</tr>
<tr>
...
</tr>

and I want this:

<tr>...</tr>
<tr>...</tr>
<tr>...</tr>

r/sed • • Oct 23 '20

Sed "unterminated s command" error

1 Upvotes

This is my sed file:

s/^\(....\).*/\1/;q

I get this error:

sed: -e expression #1, char 10: unterminated `s' command

I cannot for the life of me figure out what's wrong. I have all the / I need don't I?


r/sed • • Sep 22 '21

s command bug or intended ?

2 Upvotes

Hy sed users,

I've encountered something strange regarding the s command I was not aware of and from the documentation it's not clear this is intended or not. So I'm asking more experienced sed users for clarity.

You can replace the / (slash) character on the s command by any other character and it will still work. Eventough the man page clearly indicates s/regexp/replacement/

Examples:

$ echo "test one" | sed -e "s*one*two*"
test two
$ echo "test one" | sed -e "s%one%two%"
test two
$ echo "test one" | sed -e "s1one1two1"
test two
$ echo "test one" | sed -e "sAoneAtwoA"
test two

I'm on linux sed (GNU sed) 4.7


r/sed • • Apr 03 '19

What is wrong with my sed command

2 Upvotes

I have a file that looks like this

000099990000 Carlos C

000099990000 Ana B

000099990000 Ana A Test

000099990000 Ana B

000099990000 Carlos C

I want to find the first occurrence of "Ana B" only, then replace the 9999 part of the code with 1111.

Im running the following line:

sed -i '0,/Ana B/ s/9999/1111/' file.txt

but I'm getting the following result:

000099990000 Carlos C

000011110000 Ana B

000011110000 Ana A Test

000011110000 Ana B

000099990000 Carlos C

So sed is replacing all "Ana" AND is also not stoping on the first match. What is wrong with my command?


r/sed • • Oct 18 '21

(Noob) How can I replace these HTML tags with sed?

2 Upvotes

I need help replacing HTML tags with sed. I understand the basics of sed, that to replace "tutorial" with "example" in file.txt I need to use the following command with sed:

sed -i 's/tutorial/example/' file.txt

However if I want to replace text that uses the special characters inherent to HTML such as <, >, /, ", ?, #, etc. sed doesn't know what to do and gives me errors. I don't know how to fix this.

I am trying to use sed -i with an HTML file to replace this:

<hr><p>

with this:

<hr><p><center><a href="/?newbookmark=USK@Lxc7lUjRCpLODRAhyHqZj139qHt4ZVgf7JV6ZILCJW8,aT0vsHH51a7qkIFaQUsb3HvMv01DAkJDv5qLcljld-g,AQACAAE/APOD/-1/&desc=Astronomy+Picture+of+the+Day&hasAnActivelink=true">Bookmark APOD</a></center></p><p>

Unfortunately this gives me errors. Any idea how to fix this? Thanks!


r/sed • • Apr 20 '25

Change HTML text daily with sed in cron?

3 Upvotes

Greetings!

I am not new to Linux, but I do not use sed often, if at all, which is why I am asking here! I have a Proxmox server with several VM's running DietpiOS. One of the VM's is running Caddy Web Server.

I want to change the date in an HTML file daily. I want to change the date, in an HTML file, that is tied to a javascript that automates copying text to the clipboard by clicking on a hyperlink.

Below is the line of code, and all I want is to either have a sed command that I can add in a script or a cronjob to change to the current date, so I do not have to manually edit the file myself:

<a href="" class="copy-click" data-copy-string="Power Outage 4/23 @ "

data-tooltip-text="Click to copy" data-tooltip-text-copied="✔ Copied to clipboard">

Power Outage </a><br>

The file is located in:

/var/www/html/sr_guy/index.html

***EDIT***

I am half-way there, I used ChatGPT, and I figured out how to add the date:

sed -i "s|data-copy-string=\"Power Outage @ |&$(date +'%m-%d') |" index.html

Creates this output: Power Outage @ 04-20

My new problem, I need delete the date for the next day date change, before I can add the new date, what's the best way to accomplish that?

**EDIT #2**

I figured it out. I added the keywords "power-boo" and "date-bing" in the html, and replaced those words with my desired phrase and current date with the following sed commands in a script:

#Date

sed -i "s/power-boo/Power Outage $(date '+%m-%d') @/g" /var/www/html/miked/index.html

#Time#sed -i "s/date-bing/$(date '+%H:%M')/g" /var/www/html/miked/index.html


r/sed • • Jun 08 '22

Replace variable in [] with sed.

3 Upvotes

i have a curl statement that looks like this.

jq ".results[$a], .results[$b], .results[$c]"

a=6 b=7 c=8

How can i tell sed to only replace specific column with the desired variable.

ive tried this sed -i 's/[$a:.[]]]/[$a]/g'

Appreciate any help or recommendations


r/sed • • Mar 28 '21

Append character to line n with sed (or other tool)

3 Upvotes

I would simply like to append a "#" character to line 2 in a file I have.

What would the syntax for this be?

Someone suggested sed -i '2 s/./#&/', which I understand to say, "replace any first character with #&". They say "&" will be replaced by whatever was matched by ".".

This will work, but it doesn't seem elegant. Is it necessary to do it so, in sed? Or is there any specific operator that appends more directly? Any true "append" operations?

Thanks very much.


r/sed • • Feb 09 '20

Using sed to cut up a log file

4 Upvotes

I am tailing a log file and using grep to cut out just the lines that I want info for, now I want to pipe that into sed to trim the fat per se.

For example the original log is:

Feb  9 17:48:21 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21 
Feb  9 17:48:21 dnsmasq[884]: forwarded captive.g.aaplimg.com to 8.8.4.4 
Feb  9 17:48:21 dnsmasq[884]: reply captive.g.aaplimg.com is 17.253.55.202 
Feb  9 17:48:21 dnsmasq[884]: reply captive.g.aaplimg.com is 17.253.55.204 

Then i use grep --line-buffered "query" to get just the query lines:

Feb  9 18:42:21 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21 
Feb  9 18:42:40 dnsmasq[884]: query[A] sb.scorecardresearch.com from 192.168.178.21 Feb  9 18:42:51 dnsmasq[884]: query[A] captive.g.aaplimg.com from 192.168.178.21 
Feb  9 18:43:06 dnsmasq[884]: query[A] captive-cidr.origin-apple.com.akadns.net from 192.168.178.21 
Feb  9 18:43:06 dnsmasq[884]: query[AAAA] captive-cidr.origin-apple.com.akadns.net from 192.168.178.21 
Feb  9 18:43:21 dnsmasq[884]: query[A] time-macos.apple.com from 192.168.178.21 

Now I have as a command:

sudo tail -F /var/log/pihole.log  | grep --line-buffered "query" | sed -E 's/(\query).*(\from)/\1 \2/' 

Because I want to cut out elements so it goes to:

18:42 captive.g.aaplimg.com 
18:42 sb.scorecardresearch.com 
18:42 captive.g.aaplimg.com 
18:43 captive-cidr.origin.apple.com  

and so forth.

Where am i going wrong?


r/sed • • Jan 19 '26

Join multiple lines by a number?

4 Upvotes

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 magic

I 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-blah

Before 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 • • Mar 18 '23

Execute sed command remotely using remote env variable

4 Upvotes

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 • • Jan 31 '23

Print just first lines beginning with "> " via sed doesn't work

4 Upvotes

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-test

Note 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.


r/sed • • Aug 21 '22

replace something only on the first line

4 Upvotes

i am creating a script to check a script with shellcheck which apply's, i am wondering how to replace

/bin/sh

with

/usr/bin/env sh

only on the first line


r/sed • • Sep 03 '22

Trying to find all instances of a word between two other words

4 Upvotes

I am trying to search for a problematic coding practice.

I am looking to find the file and line number everywhere a command 'target_text' is used between the text 'start_text' and 'end_text'.


r/sed • • Aug 18 '22

have a python program that left every () absent fron print("") how can I use sed to add a () around every ""? Keep in mind there is a string between every "" varying in length. Thank you.

4 Upvotes

r/sed • • Mar 08 '22

sed to add two forward slashes to the beginning of a line in a file

5 Upvotes

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.php

which results in:

sed: -e expression #1, char 29: unterminated `s' command

Another attempt is to escape out the (') instead of quoting it out:

sed -i 's|$cfg['\''Temp|\/\/$cfg['\''Temp|g' test.php

Which results in the same error.

Any help is appreciated!


r/sed • • Mar 01 '22

breaking up a long wordlike string

6 Upvotes

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
zz

Currently 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 • • Oct 09 '25

sed made it easy

7 Upvotes

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!


r/sed • • Sep 23 '20

should be simple, but...

3 Upvotes

I just want to reformat a text file from

name
date

to

name    date

where the date is in month-day-year format


r/sed • • Apr 19 '18

combining sed commands

3 Upvotes

Can anyone help? I need to run three commands but I don't want to have to create all of the temp files.

sed 's/rows will be truncated//g' pipe_test.txt > pipe_test2.txt

sed 's/[[:space:]]|[[:space:]]/|/g' pipe_test2.txt > pipe_test3.txt

sed '/\s*$/d' pipe_test3.txt > pipe_test4.txt

Thanks!