Questions tagged [sed]

"sed" ("stream editor") is a Unix utility that parses and transforms text files.

"sed" (stream editor) is a Unix utility that parses and transforms text files.

It implements a fairly basic programming language to perform the transformations, but it is in fact Turing-complete.

Resources

sed on Wikipedia

1058 questions
325
votes
6 answers

How to match whitespace in sed?

How can I match whitespace in sed? In my data I want to match all of 3+ subsequent whitespace characters (tab space) and replace them by 2 spaces. How can this be done?
Peter Smit
  • 9,636
209
votes
18 answers

Removing ANSI color codes from text stream

Examining the output from perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' in a text editor (e.g., vi) shows the following: ^[[37mABC ^[[0m How would one remove the ANSI color codes from the output file? I…
user001
  • 3,994
175
votes
4 answers

How to match digits followed by a dot using sed?

I'm trying to use sed to substitute all the patterns with digits followed immediately by a dot (such as 3., 355.) by an empty string. So I try: sed 's/\d+\.//g' file.txt But it doesn't work. Why is that?
Mika H.
  • 1,939
136
votes
20 answers

Substitution in text file **without** regular expressions

I need to substitute some text inside a text file with a replacement. Usually I would do something like sed -i 's/text/replacement/g' path/to/the/file The problem is that both text and replacement are complex strings containing dashes, slashes,…
Andrea
  • 1,555
93
votes
6 answers

use of alternation "|" in sed's regex

I am using sed, GNU sed version 4.2.1. I want to use the alternation "|" symbol in a subexpression. For example : echo "blia blib bou blf" | sed 's/bl\(ia|f\)//g' should return " blib bou " but it returns "blia blib bou blf". How can I have the…
Cedric
  • 1,123
92
votes
3 answers

How to use 'sed' with piping

I want to replace a string outputted from grep, I have: $ npm info webpack | grep version it outputs me $ version: '2.1.0-beta.12', but I want to have: $ 2.1.0-beta.12 So I think I might achieve that using sed and replace unnecessary substrings.…
Oskar Szura
  • 1,045
68
votes
7 answers

How to mass prepend text to file names?

Lets say I have a directory full of .md files all named various things. Lets say I wanted to prepend "test" to the front of each file name. So for example: file a.md, b.md, and c.md would become test - a.md, test - b.md, and test - c.md. How would I…
66
votes
7 answers

Newlines in sed on Mac OS X

I find that \n doesn't work in sed under Mac OS X. Specifically, say I want to break the words separated by a single space into lines: # input foo bar I use, echo "foo bar" | sed 's/ /\n/' But the result is stupid, the \n is not…
Ivan Xiao
  • 2,945
55
votes
4 answers

How to use sed to remove null bytes?

What is the sed incantation to remove null bytes from a file? I'm trying: s/\000//g but that is stripping out strings of zeroes. s/\x00//g seems to have no effect. I'm trying to do this in a sed script, so I'm not sure the echo trick will work.
Chris Curvey
  • 1,537
52
votes
11 answers

sed: how to replace line if found or append to end of file if not found?

With a single input file that only contains comments (starting with #) and VARIABLE=value lines, is it possible to replace a value for a single variable if found and, otherwise, append the pair to the end of file if not found? My current method…
BlakBat
  • 1,279
34
votes
3 answers

How to replace line in file with pattern with sed?

I'm reading a lot of documentation on sed, and am still stumped on my particular use case. I want to replace this line in a conf file with my own line: Replace this line: #maxmemory with: maxmemory 26gb This is what I tried: sed…
32
votes
12 answers

Efficiently remove the last two lines of an extremely large text file

I have a very large file (~400 GB), and I need to remove the last 2 lines from it. I tried to use sed, but it ran for hours before I gave up. Is there a quick way of doing this, or am I stuck with sed?
31
votes
18 answers

List only the device names of all available network interfaces

I want to get a list of all available Network-Device Names on my Linux server. I figured that ifconfig would do the job, however ifconfig produces quite much output: eth0 Link encap:Ethernet Hardware Adresse 08:00:27:fc:5c:98 inet…
27
votes
1 answer

What does `-n` option in sed do?

This is the man page entry for -n: -n suppress automatic printing of pattern space I notice that when not using -n for certain operations, each line is printed to stdout (and the requested lines are printed twice): $ cat test.txt…
dotancohen
  • 11,720
24
votes
7 answers

How to print last character of a file

In Unix, using a simple command like sed, is there a way to print the last character of a file?
suvitha
1
2 3
70 71