Questions tagged [sed]

sed is a command-line stream editor for filtering and transforming text.

Filter by
Sorted by
Tagged with
947 votes
10 answers

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 ...
terdon's user avatar
  • 235k
444 votes
2 answers

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 ...
roo's user avatar
  • 4,601
431 votes
18 answers

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 ...
Belmin Fernandez's user avatar
401 votes
4 answers

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 ...
detly's user avatar
  • 5,030
224 votes
4 answers

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 ...
Daniel Jonsson's user avatar
219 votes
7 answers

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]]></...
Harry Muscle's user avatar
  • 2,437
202 votes
6 answers

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 ...
MaQleod's user avatar
  • 2,614
170 votes
11 answers

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
kickass13's user avatar
  • 1,773
158 votes
16 answers

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 ...
Manolo's user avatar
  • 1,717
154 votes
19 answers

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 and on the ...
DisplayName's user avatar
  • 11.5k
136 votes
4 answers

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: ...
Javier Villanueva's user avatar
133 votes
14 answers

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&...
Christos Baziotis's user avatar
126 votes
12 answers

how to rename multiple files by replacing string in file name? this string contains a "#" ...
user avatar
121 votes
7 answers

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: ...
JigarGandhi's user avatar
  • 4,830
114 votes
8 answers

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?
user avatar
112 votes
7 answers

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, ...
Macha's user avatar
  • 3,770
107 votes
7 answers

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:...
WernerCD's user avatar
  • 1,314
105 votes
6 answers

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 '{...
user70573's user avatar
  • 1,289
105 votes
5 answers

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 ...
Peter Grill's user avatar
  • 1,583
101 votes
5 answers

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 ...
jakub.g's user avatar
  • 3,153
95 votes
7 answers

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 | ...
dmgl's user avatar
  • 1,083
94 votes
3 answers

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 ...
Folaht's user avatar
  • 972
91 votes
12 answers

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 ...
loganaayahee's user avatar
  • 1,109
89 votes
9 answers

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 ...
Jim's user avatar
  • 9,780
89 votes
13 answers

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": [],...
user3086014's user avatar
86 votes
2 answers

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 ...
krypto's user avatar
  • 961
84 votes
6 answers

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 ...
mtk's user avatar
  • 26.9k
83 votes
4 answers

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/'
Howard's user avatar
  • 977
77 votes
2 answers

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" ...
devopsfun's user avatar
  • 1,377
71 votes
4 answers

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) ...
Nainita's user avatar
  • 2,732
70 votes
1 answer

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'
Tyilo's user avatar
  • 5,911
67 votes
9 answers

How can I use variables in the LHS and RHS of a sed substitution?

I want to do: cat | sed 's/old_name/new_name/' > in my program. But I want to use variables, e.g. old_run='old_name_952' new_run='old_name_953' I have ...
Michael Durrant's user avatar
66 votes
6 answers

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 '...
Gregg Leventhal's user avatar
66 votes
6 answers

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?
Andrew Bolster's user avatar
65 votes
4 answers

Removing leading zeros from date output

I made an alias of the date command to display date in the following format: 2013. using this command: alias date = date +"%Y.%m.%d.%H.%M.%S" Everything works great, except I want ...
easl's user avatar
  • 1,978
64 votes
3 answers

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 ...
Melab's user avatar
  • 3,838
63 votes
1 answer

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 ...
Amir Afghani's user avatar
  • 7,103
62 votes
10 answers

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 ...
iconoclast's user avatar
  • 9,077
59 votes
7 answers

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 ...
Red's user avatar
  • 1,422
59 votes
3 answers

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 ...
Spike's user avatar
  • 989
57 votes
11 answers

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-...
user1228191's user avatar
57 votes
9 answers

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 ...
Amelio Vazquez-Reina's user avatar
57 votes
3 answers

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
BntMrx's user avatar
  • 681
56 votes
3 answers

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/)....
Michael Durrant's user avatar
54 votes
3 answers

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- but I want to get the following output ( ...
maihabunash's user avatar
  • 7,001
52 votes
4 answers

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 ...
Jay Kah's user avatar
  • 521
51 votes
5 answers

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)...
user avatar
51 votes
3 answers

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 != ...
munish's user avatar
  • 7,825
50 votes
4 answers

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.
yihyoon's user avatar
  • 501
49 votes
9 answers

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 ...
Nainita's user avatar
  • 2,732

2 3 4 5