Questions tagged [sed]
sed is a command-line stream editor for filtering and transforming text.
7,216
questions
947
votes
10
answers
1.8m
views
How can I replace a string in a file(s)?
Replacing strings in files based on certain search criteria is a very common task. How can I
replace string foo with bar in all files in the current directory?
do the same recursively for sub ...
444
votes
2
answers
1.9m
views
Using 'sed' to find and replace [duplicate]
I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an ...
431
votes
18
answers
568k
views
How can I use sed to replace a multi-line string?
I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example:
$ cat > alpha.txt
This is
a test
Please do not
be alarmed
$ sed -i'.original' 's/a test\nPlease ...
401
votes
4
answers
726k
views
What characters do I need to escape when using sed in a sh script?
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
224
votes
4
answers
220k
views
What does <<< mean? [duplicate]
What does <<< mean? Here is an example:
$ sed 's/a/b/g' <<< "aaa"
bbb
Is it something general that works with more Linux commands?
It looks like it's feeding the sed program with ...
219
votes
7
answers
660k
views
Using sed to find and replace complex string (preferrably with regex)
I have a file with the following contents:
<username><![CDATA[name]]></username>
<password><![CDATA[password]]></password>
<dbname><![CDATA[name]]></...
202
votes
6
answers
688k
views
Return only the portion of a line after a matching pattern
So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a ...
170
votes
11
answers
467k
views
Delete First line of a file
How can I delete the first line of a file and keep the changes?
I tried this but it erases the whole content of the file.
$sed 1d file.txt > file.txt
158
votes
16
answers
434k
views
How to insert text before the first line of a file?
I've been looking around sed command to add text into a file in a specific line.
This works adding text after line 1:
sed '1 a\
But I want to add it before line 1. It would be:
sed '0 a\
but I get ...
154
votes
19
answers
161k
views
Decoding URL encoding (percent encoding)
I want to decode URL encoding, is there any built-in tool for doing this or could anyone provide me with a sed code that will do this?
I did search a bit through unix.stackexchange.com and on the ...
136
votes
4
answers
497k
views
How to insert text after a certain string in a file?
Right now I'm using
echo "Hello World" >> file.txt
to append some text to a file but I also need to add text below a certain string let's say [option], is it possible with sed?
EG:
...
133
votes
14
answers
33k
views
Replace string in a huge (70GB), one line, text file
I have a huge (70GB), one line, text file and I want to replace a string (token) in it.
I want to replace the token <unk>, with another dummy token (glove issue).
I tried sed:
sed 's/<unk&...
126
votes
12
answers
357k
views
how to rename multiple files by replacing string in file name? this string contains a "#"
https://serverfault.com/questions/70939/how-to-replace-a-text-string-in-multiple-files-in-linux
https://serverfault.com/questions/228733/how-to-rename-multiple-files-by-replacing-word-in-file-name
...
121
votes
7
answers
213k
views
Convert file contents to lower case
I have temp file with some lower-case and upper-case contents.
Input
Contents of my temp file:
hi
Jigar
GANDHI
jiga
I want to convert all upper to lower.
Command
I tried the following command:
...
114
votes
8
answers
314k
views
SED: insert text after the last line?
This sed command inserts a tag to the beginning of a file:
sed -i "1s/^/<?php /" file
How can I insert something to the end of each file with sed?
112
votes
7
answers
71k
views
Is there a basic tutorial for grep, awk and sed? [closed]
I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, ...
107
votes
7
answers
355k
views
How to strip multiple spaces to one using sed?
sed on AIX is not doing what I think it should.
I'm trying to replace multiple spaces with a single space in the output of IOSTAT:
# iostat
System configuration: lcpu=4 drives=8 paths=2 vdisks=0
tty:...
105
votes
6
answers
449k
views
How to use a shell command to only show the first column and last column in a text file?
I need some help to figure out how to use the sed command to only show the first column and last column in a text file. Here is what I have so far for column 1:
cat logfile | sed 's/\|/ /'|awk '{...
105
votes
5
answers
112k
views
Differences between sed on Mac OSX and other "standard" sed?
I am having some issues in using an answer provided on this site for this question about a sed command to replace a blank line with two other lines of content, and it was brought up if the sed command ...
101
votes
5
answers
246k
views
Remove line containing certain string and the following line
I use this
cat foo.txt | sed '/bar/d'
to remove lines containing the string bar in the file.
I would like however to remove those lines and the line directly after it. Preferably in sed, awk or ...
95
votes
7
answers
251k
views
How to read first and last line from cat output?
I have text file. Task - get first and last line from file after
$ cat file | grep -E "1|2|3|4" | commandtoprint
$ cat file
1
2
3
4
5
Need this without cat output (only 1 and 5).
~$ cat file | ...
94
votes
3
answers
207k
views
Can sed save its output to a file?
> can do it.
echo "text" > file
tee can do it.
echo "test" | tee file
Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without ...
91
votes
12
answers
171k
views
How to print all lines after a match up to the end of the file?
Input file1 is:
dog 123 4335
cat 13123 23424
deer 2131 213132
bear 2313 21313
I give the match the pattern from in other file ( like dog 123 4335 from file2).
I match the pattern of the line is ...
89
votes
9
answers
279k
views
Can sed replace new line characters?
Is there an issue with sed and new line character?
I have a file test.txt with the following contents
aaaaa
bbbbb
ccccc
ddddd
The following does not work:
sed -r -i 's/\n/,/g' test.txt
...
89
votes
13
answers
513k
views
How to parse JSON with shell scripting in Linux?
I have a JSON output from which I need to extract a few parameters in Linux.
This is the JSON output:
{
"OwnerId": "121456789127",
"ReservationId": "r-48465168",
"Groups": [],...
86
votes
2
answers
231k
views
Replacing string based on line number
I have a situation where i want to replace a particular string in many files
Replace a string AAA with another string BBB but there are lot of strings starting with AAA or ending in AAA ,and i want ...
84
votes
6
answers
225k
views
Delete last line from the file
I use sed to quickly delete lines with specific position as
sed '1d'
sed '5d'
But, what if I want to delete the last line of the file and I don't know the count of lines (I know I can get that ...
83
votes
4
answers
131k
views
What is the purpose of -e in sed command?
I can't find any documentation about the sed -e switch, for simple replace, do I need it?
e.g.
sed 's/foo/bar/'
VS
sed -e 's/foo/bar/'
77
votes
2
answers
250k
views
Slash and backslash in sed [duplicate]
I want to use sed to change a slash into a backslash and a slash, i.e. / -> \/. But it does not work. Here a small example:
#!/bin/bash
TEST=/etc/hallo
echo $TEST
echo $TEST | sed "s/hallo/bello/g"
...
71
votes
4
answers
88k
views
How to add line numbers in every line using shell command?
My file,
PSS-A (Primary A)
PSS-B (Primary B)
PSS-C (Primary C)
PSS-D (Primary D)
PSS-E (Primary E)
PSS-F (Primary F)
PSS-G (Primary G)
PSS-H (Primary H)
PSS-I (Primary I)
SPARE (SPARE)
...
70
votes
1
answer
230k
views
Only return the matched string in sed [duplicate]
Possible Duplicate:
Extracting a regex matched with 'sed' without printing the surrounding characters
How do I make this only print test:
echo "atestb" | sed -n 's/\(test\)/\1/p'
67
votes
9
answers
263k
views
How can I use variables in the LHS and RHS of a sed substitution?
I want to do:
cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh
in my program.
But I want to use variables, e.g.
old_run='old_name_952'
new_run='old_name_953'
I have ...
66
votes
6
answers
101k
views
Regex alternation/or operator (foo|bar) in GNU or BSD Sed
I cannot seem to make it work. GNU sed documentation says to escape the pipe, but that doesn't work, nor does using a straight pipe without the escape. Adding parens makes no difference.
$ echo '...
66
votes
6
answers
25k
views
Replacing Multiple blank lines with a single blank line in vim / sed
Question more or less says it all. I'm aware that /^$/d will remove all blank lines, but I can't see how to say 'replace two or more blank lines with a single blank line'
Any ideas?
65
votes
4
answers
50k
views
Removing leading zeros from date output
I made an alias of the date command to display date in the following format:
2013.06.14.12.10.02
using this command:
alias date = date +"%Y.%m.%d.%H.%M.%S"
Everything works great, except I want ...
64
votes
3
answers
134k
views
sed capture groups not working
I have a string of the format [0-9]+\.[0-9]+\.[0-9]. I need to extract the first, second, and third numbers separately. As I understand it, capture groups should be capable of this. I should be able ...
63
votes
1
answer
129k
views
find & sed (search and replace)
I'm using the following command on my mac:
$find . -name “*.java” -exec sed -i ’s/foo/bar/g’ {} \;
and it seems to have no effect.
I have two files in the directory that end in .java, which both ...
62
votes
10
answers
104k
views
Simple sed replacement of tabs mysteriously failing
This ought to be really simple, but for some reason it is not working:
sed -i.bak -E 's/\t/ /' file.txt
Instead of replacing tab characters, it's replacing t characters. I've tried every variation ...
59
votes
7
answers
20k
views
How can I achieve portability with sed -i (in-place editing)?
I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...
59
votes
3
answers
176k
views
How to grep lines between start and end pattern? [duplicate]
I have a file which is having following content:
zdk
aaa
b12
cdn
dke
kdn
Input1: aaa and cdn
Output 1:
aaa
b12
cdn
Input 2: zdk and dke
Output 2:
zdk
aaa
b12
cdn
dke
I could use below commands ...
57
votes
11
answers
109k
views
How to insert the content of a file into another file before a pattern (marker)?
File1 contents:
line1-file1 "1"
line2-file1 "2"
line3-file1 "3"
line4-file1 "4"
File2 contents:
line1-file2 "25"
line2-file2 "24"
Pointer-file2 "23"
line4-...
57
votes
9
answers
127k
views
Using sed to convert newlines into spaces
Say I have a shell variable $string that holds some text with several newlines, e.g.:
string="this
is
a test"
I would like to convert this string into a new string new_string where all line breaks ...
57
votes
3
answers
188k
views
Why does sed outputs "char 53: unterminated `s' command"
Why this bash script
ssh $SERVER bash <<EOF
sed -i "s/database_name: [^ ]*/database_name: kartable_$ME" $PARAM_FILE
exit
EOF
output ->
sed: -e expression #1, char 53: unterminated `s' command
56
votes
3
answers
71k
views
sed - how to do several consecutive substitutions but process file only once?
If I am doing several substitutions which need to be consecutive, e.g.
sed -i '/^[[:space:]]*browser.*\.should/s/browser/expect(browser/' t1_spec.rb
sed -i '/expect(browser.*\.should/s/\.should/)....
54
votes
3
answers
115k
views
How do I replace the last occurrence of a character in a string using sed?
How do I replace only the last occurrence of "-" in a string with a space using sed?
For example:
echo $MASTER_DISK_RELEASE
swp-RedHat-Linux-OS-5.5.0.0-03
but I want to get the following output ( ...
52
votes
4
answers
95k
views
sed - replace string with file contents
I have two files: file1 and file2.
file1 has the following contents:
---
host: "localhost"
port: 3000
reporter_type: "zookeeper"
zk_hosts:
- "localhost:2181"
file2 contains an IP ...
51
votes
5
answers
92k
views
How to "grep" for line length in a given range?
NOTE: This question is the complement of this Q&A: How to "grep" for line length *not* in a given range?
I need to get only the lines from a textfile (a wordlist, separated with newline)...
51
votes
3
answers
132k
views
Removing all spaces, tabs, newlines, etc from a variable?
This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel).
I am getting spaces in that variable.
+ 0 !=
...
50
votes
4
answers
224k
views
How to use Sed to replace all characters before colon?
How do I replace the following string
hd_ma_prod_customer_ro:*:123456789:john.doe
with
john.doe
Basically I need to look for the last colon (:) and delete everything before and including it.
49
votes
9
answers
154k
views
How do I delete the first n lines and last line of a file using shell commands?
I have a file named Element_query containing the result of a query :
SQL> select count (*) from element;
[Output of the query which I want to keep in my file]
SQL> spool off;
I want to ...