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:

693 questions
560
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,157
  • 38
  • 78
  • 102
57
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
  • 14,548
  • 28
  • 104
  • 178
55
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,071
  • 7
  • 26
  • 32
49
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,157
  • 38
  • 78
  • 102
40
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
  • 4,744
  • 10
  • 37
  • 48
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,071
  • 7
  • 26
  • 32
28
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
  • 2,932
  • 12
  • 67
  • 115
23
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
  • 57,282
  • 43
  • 144
  • 200
22
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,315
  • 1
  • 15
  • 30
21
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?
user3069326
  • 716
  • 4
  • 10
  • 18
18
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…
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 =…
Felipe Caldas
  • 263
  • 1
  • 2
  • 5
15
votes
7 answers

Regex with sed command to parse json text

I have this json text: { "buildStatus" : { "status" : "ERROR", "conditions" : [{ "status" : "OK", "metricKey" : "bugs" }, { "status" : "ERROR", …
user1876040
  • 253
  • 1
  • 2
  • 4
15
votes
6 answers

Which is faster to delete first line in file... sed or tail?

In this answer (How can I remove the first line of a file with sed?) there are two ways to delete the first record in a file: sed '1d' $file >> headerless.txt ** ---------------- OR ----------------** tail -n +2 $file >> headerless.txt Personally…
WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401
15
votes
5 answers

Difference of two big files

I have "test1.csv" and it contains 200,400,600,800 100,300,500,700 50,25,125,310 and test2.csv and it contains 100,4,2,1,7 200,400,600,800 21,22,23,24,25 50,25,125,310 50,25,700,5 now diff test2.csv test1.csv > result.csv is different than diff…
Lynob
  • 6,615
  • 16
  • 76
  • 108
1
2 3
46 47