Questions tagged [sed]
sed is a command-line stream editor for filtering and transforming text.
6,746
questions
913
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 ...
410
votes
2
answers
1.8m
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 ...
384
votes
18
answers
495k
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 ...
369
votes
4
answers
623k
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 ...
194
votes
4
answers
184k
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 ...
186
votes
7
answers
565k
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]]></...
180
votes
6
answers
617k
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 ...
156
votes
11
answers
441k
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
139
votes
15
answers
144k
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 ...
134
votes
15
answers
361k
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 ...
132
votes
14
answers
31k
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&...
122
votes
3
answers
449k
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:
...
111
votes
7
answers
201k
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:
...
110
votes
7
answers
69k
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, ...
109
votes
11
answers
297k
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
...
105
votes
8
answers
276k
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?
97
votes
5
answers
237k
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 ...
94
votes
7
answers
314k
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:...
90
votes
5
answers
96k
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 ...
84
votes
7
answers
238k
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 | ...
84
votes
6
answers
379k
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 '{...
83
votes
13
answers
443k
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": [],...
82
votes
3
answers
169k
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 ...
80
votes
12
answers
144k
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 ...
77
votes
6
answers
186k
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 ...
77
votes
2
answers
201k
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 ...
75
votes
9
answers
244k
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
...
71
votes
4
answers
97k
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/'
67
votes
1
answer
202k
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'
66
votes
9
answers
262k
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 ...
62
votes
2
answers
207k
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"
...
62
votes
4
answers
45k
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 ...
61
votes
10
answers
93k
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 ...
58
votes
3
answers
123k
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 ...
56
votes
6
answers
21k
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?
53
votes
11
answers
95k
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-...
53
votes
7
answers
18k
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 ...
52
votes
6
answers
80k
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 '...
52
votes
3
answers
141k
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 ...
51
votes
3
answers
155k
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
51
votes
1
answer
107k
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 ...
50
votes
4
answers
69k
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)
...
49
votes
3
answers
56k
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/)....
49
votes
3
answers
104k
views
Join multiple sed commands in one script for processing CSV file
Having a CSV file like this:
HEADER
"first, column"|"second "some random quotes" column"|"third ol' column"
FOOTER
and looking for result like:
HEADER
first, column|second "some random quotes" ...
47
votes
3
answers
101k
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 ( ...
47
votes
9
answers
142k
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 ...
46
votes
1
answer
60k
views
How to replace a left parenthesis with sed?
I have to replace ( with some character in my file, and I can't do it. \( is used for grouping in sed and when I used \\( with sed, it treated it as \( character not as just (. It seems like a tricky ...
46
votes
1
answer
69k
views
Delete specific line number(s) from a text file using sed?
I want to delete one or more specific line numbers from a file. How would I do this using sed?
45
votes
4
answers
15k
views
Reverse grepping
Let's say, I have a really big text file (about 10.000.000 lines). I need to grep it from the end and save result to a file. What's the most efficient way to accomplish task?
44
votes
13
answers
38k
views
Addition with 'sed'
I am trying to perform a mathematical operation with sed, but it continues to treat my variables as strings. The input is of this kind:
$ echo 12 | sed 's/[0-9]*/&+3/'
$ 12+3
I'd like to have 15 ...