Questions tagged [sed]

Sed is known as a Stream Editor in that it can carry out various filtering and/or transforming functions on standard input or on user specified files. It is commonly used to search-and-replace in text files. If your question is about text-processing, this tag is likely to be appropriate

The sed command can search for, print and edit text from some stream, such as a file or standard input. It is often used as a search-and-replace tool.

Ubuntu uses the GNU version of sed

If you are asking a question about text processing, this tag is probably appropriate. If you want to transform some text, it is usually necessary to give a clear example of what you have as input and the output you want to achieve.

Related tags:

708 questions
591
votes
3 answers

How do I use variables in a sed command?

I tried the following code to replace QQ with ZZ, but it doesn't do what I want: var1=QQ sed -i 's/$var1/ZZ/g' $file However, this code does what I want: sed -i 's/QQ/ZZ/g' $file How do I use variables in sed?
UAdapter
  • 17,967
60
votes
2 answers

Use sed on a string variable rather than a file

I have been told it was possible, but without a single working example, that sed can read from a string variable without need for an input file. I have yet to get it to work. For general safety, I’m writing the $PATH variable to another variable,…
j0h
  • 15,365
58
votes
3 answers

Sed to delete blank spaces

Does anyone know how to use Sed to delete all blank spaces in a text file? I haven been trying to use the "d" delete command to do so, but can't seem to figure it out
Justin
  • 2,151
  • 7
  • 29
  • 32
51
votes
2 answers

Sed replace specific line in file

I'm building a bash script for my virtual machine and I would like to know how to replace a specific line in this document: [base] ## uncomment and set autologin username to enable autologin # autologin=dgod ## uncomment and set timeout to enable…
lewis4u
  • 5,046
51
votes
3 answers

How to escape file path in SED?

I would like to replace $fileWithPath in $file, however this doesn't work because (I think) path is not escaped. How to escape it? sed -i 's/${fileWithPath}/HAHA/g' $file
UAdapter
  • 17,967
38
votes
4 answers

sed with PCRE (like grep -P)

I am happy that grep does support Perl Compatible Regular Expressions with the -P option. Is there a reason why the tool sed does not have this feature?
guettli
  • 1,765
34
votes
1 answer

Appending to end of a line using 'sed'

I have a line that says... Fred Flintstone, Bedrock USA and I want it to look like... Fred Flintstone, Bedrock USA *** How do I append a few * to the end of the line using sed command?
Justin
  • 2,151
  • 7
  • 29
  • 32
24
votes
5 answers

How can I merge files on a line by line basis?

cat file1 foo ice two cat file2 bar cream hundred Desired output: foobar icecream twohundred file1 and file2 will always have the same amount of lines in my scenario, in case that makes things easier.
TuxForLife
  • 1,333
24
votes
4 answers

What is sed and what is it used for?

I've been seeing a lot of sed lately, and I find it to be a rather confusing command. The manpages weren't particularly helpful, but I do know that it can be used for parsing the output of other commands. What exactly is sed and what are it's uses?…
Seth
  • 59,332
22
votes
6 answers

Grep beginning of line

I have a file with the following contents: (((jfojfojeojfow // hellow_rld (((jfojfojeojfow // hellow_rld How can I extract every line that starts with a parenthesis?
20
votes
3 answers

How to insert multiple lines with sed

I want to add this #this ##is my text before the line the specific line I tried this sed -i '/the specific line/i \ #this ##is my text ' text.txt but it only adds in 'text'. I also tried various combinations with \ and " " but nothing…
17
votes
5 answers

How to delete lines starting with certain strings

Is there a way to delete lines starting with certain strings. I have this youtube-dl code youtube-dl --extract-audio --audio-quality 0 --newline --audio-format mp3 https://www.youtube.com/playlist?list=PL1C815DB73EC2678E and its result is like…
16
votes
11 answers

Remove the first part of a string using sed

How do I get from this: randomcollege-nt\user90 to this: user90 using sed?
Roboman1723
  • 2,975
  • 8
  • 26
  • 32
16
votes
2 answers

How to sed and replace string with a folder path

I am trying to write the following bash script: HOME_DIR=/opt/my_home find ./CONFIG -type f -exec sed -i "s/_HOME_DIR_/$_HOME_DIR/g" {} \; The line that I want to be changed in the files is this: users =…
16
votes
3 answers

How do I get the exit status when using the sed command?

The grep command gives out an exit status: $echo "foo.bar" | grep -o foo foo $echo $? 0 $echo "foo.bar" | grep -o pop $echo $? 1 But I need to use sed and I realized that it has no exit status: $echo "foo.bar" | sed 's/bar.*$//' foo. $echo…
Josef Klimuk
  • 1,636
1
2 3
47 48