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

learn more… | top users | synonyms

2
votes
2answers
378 views

Stream error logs

I want to stream the contents of an error log (/log/spo.error) as it's written, but pipe the stream through sed before I see it. How can I do that?
7
votes
3answers
4k views

How do I grep without leading whitespaces?

I'm greping through a large codebase, and leading whitespaces and tabulation seem to be quite annoying. Is there any way to get rid of it? grep -R "something" ./ E.g, instead of: foo/bar.cpp: ...
8
votes
2answers
8k views

Replace string with contents of a file using sed

I have two different files: File1 /home/user1/ /home/user2/bin /home/user1/a/b/c File2 <TEXT1> <TEXT2> I want to replace the <TEXT1> of File2 with the contents of File1 ...
4
votes
2answers
2k views

Make sed ask for confirmation before each replacement?

Is there a way to make sed ask me for confirmation before each replace? Something similar to 'c' when using replace inside vim. Does sed do this at all?
3
votes
3answers
1k views

Using sed to edit lines in a file with a variable

I need to edit lines in a file using sed. Now the problem is I am replacing a particular pattern with a combination of text and number. This number is a variable which keeps on incrementing for every ...
4
votes
3answers
589 views

How do I delete all lines with “.png” in a file?

I've a list of urls of which some point to images: http://s.thebrighttag.com/iframe?c=A5lqOqP ...
2
votes
1answer
532 views

How do I replace certain text with the respective line number using sed?

I have a file where certain (not all) lines look like so: Sequence: n I wish to replace n with the respective line number. How can I do it with sed?
2
votes
2answers
1k views

Bash color output fails

Is it sed or echo problem? What I'm doing wrong? $> cat ~/bin/color_test.sh #!/bin/bash ColorOff='\e[0m' # Text Reset BWhite='\e[1;37m' # Bold White string="test TEST test" echo -e ...
9
votes
4answers
24k views

How to strip multipe 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 ...
4
votes
4answers
2k views

How to echo an escaped string

How can I echo an escaped string that contains $ in Bourne Shell? user@server:~$ cat test.sh #!/bin/sh echo $1 user@server:~$ ./test.sh \$sad\$test $sad$test I want it to return an escaped ...
3
votes
2answers
2k views

sed: delete all occurrences of a string except the first one

I have a logfile with timestamps in it. Occasionally there are multiple timestamps in one line. Now I would like to remove all of the timestamps from a line but keep the first one. I can do ...
3
votes
1answer
598 views

Why can't I sed two [or more..] empty lines to one empty line?

Why can't I sed two [or more..] empty lines to one empty line? What is the trick? echo -e "hello\n\n\nhello2" | sed 's/^$\n^$/\n/g' hello hello2
2
votes
3answers
757 views

Change character position with sed

The file contains: dateutkfilename25012009 I want to change the position of character 16th to 17th with 18th to 19th. And then change position of character 16th to 19th with 20th to 23rd... so it ...
2
votes
3answers
193 views

Facing problem with Regex Inside Sed Command

Suppose I run the following commands: export STR="abcdef.ghijkl.mnopqr.stuvwy.log" echo $STR | sed 's/\.[^.]*$//' I am getting the following result: abcdef.ghijkl.mnopqr.stuvwy Please help me ...
2
votes
1answer
208 views

Rename beginning of file name using specific text in the file itself

I have many files in directory (file1.txt, file2.txt, ...) Every files contains bunch of lines. One of the line is specific text thetext followed by random number just like thetext56, thetext21, etc. ...
2
votes
2answers
203 views

Move one file at a time based from specific number in the files

Based from this question, The HTML files date format has been changed. All of the HTML files have the date such like below: 20110730dateishere dateishere is intentionally added by me in the files, ...
7
votes
1answer
303 views

Change date “July 29th, 2011” to “20110729”

I have lots of HTML files contains date with format July 29th, 2011 I want to change date format July 29th, 2011 to 20110729, December 9th, 2010 to 20101209, etc. I think sed may works but still ...
2
votes
4answers
4k views

Deleting Lines Containing a Specified String in Different Files

Suppose there are two files in web/ named foo.php and bar.php. The 1st line of foo.php is "sdajgeSTRINGdsad" and the 10th line of bar.php is "gdfhu98324STRING". The task is to first locate these two ...
3
votes
2answers
4k views

Show only text between 2 matching pattern

I'm going to submit form using cURL, where some of the contents is come from other file, selected using sed If param1 is line matching pattern from other file using sed, below command will works ...
5
votes
1answer
413 views

Sed not working as intended

I would like to use this script with the intention of replacing all instances of the letter Q within a file with the contents of the file "question.txt". Instead what happens is that all the instances ...
3
votes
1answer
745 views

Chinese characters instead of Latin being written to file

When I run sed like this and print to a console everything is fine: sed '/Q/{ s/Q//g r /Users/ericbrotto/Desktop/question.txt }' Commision.txt But when I do this and output t a file: sed '/Q/{ ...
3
votes
2answers
943 views

sed script to insert line after the last matching line in a file

I'd like to write a script in sed to insert line to a file after all similar lines in the file. So I need to locate the last match for a pattern and insert immediately after it. It's very unlikely ...
1
vote
2answers
978 views

Copy a line matching pattern to beside another matching pattern line using sed

I want to copy the line containing www.website.com into the line containing thetext. For example, I want to transform this input: Lorem ipsum dolor sit amet, elit. Cras eu www.website.com velit ...
2
votes
1answer
232 views

reading input in sed

i'm new to shell scripting. I'm learning little by little. I'm more familiar with php which is working toward my disadvantage right now. Anyway, i'm writing a shell script and i read in input with ...
6
votes
2answers
415 views

Which sed version is not GNU sed 4.0?

I'm trying to figure out which version of sed that I've got on BusyBox 1.18.3. The --version output is cryptic. $ sed --version This is not GNU sed version 4.0 The following code is from the ...
4
votes
1answer
437 views

Are there zero-width assertions in sed?

Is it possible to make lookahead or lookbehind zero-width assertions in sed? I want to emulate Perl's (?=) and family. My sed is not GNU sed version 4.0.
2
votes
1answer
176 views

sed one-liner to delete word-medial spaces

I have a text file that consists of some N ORMAL E NGLISH W ORDS in all-caps. Unfortunately, these words all have a space between the first and following letters, which is not acceptable. I tried ...
2
votes
2answers
114 views

SED misbehaving with input file

Simply put I have a small sed command that is supposed to trim " and wrap the input between the <id> tag. It goes well single line and multiple line input, but badly misbehaves on an input file ...
3
votes
2answers
627 views

How do remove identical lines in one file from another, using sed?

I have two files, one being a superset of the other. I want to remove the identical lines in the smaller files from the larger file. One possible complication is that the lines contain backslashes. ...
5
votes
3answers
3k views

The way to use `/usr/bin/env sed -f ` in shebang?

Typing /usr/bin/env sed -f in terminal works. But if use it as a shebang, #!/usr/bin/env sed -f s/a/b/ The script will be fail to execute: /usr/bin/env: sed -f: No such file or directory I ...
3
votes
2answers
7k views

sed one-liner to delete everything between a pair of brackets?

I am working with some text that is full of stuff between brackets [] that I don't want. Since I can delete the brackets myself, I don't need the one-liner to do that for me, but I do need a one-liner ...
0
votes
4answers
417 views

Sed regex - include original matching

INPUT: dsfgsdf8gfsd 2011.06.26. v iudsfg98sdfg sosdufgsdfg 2011.06.27. h 8xdofguiosdfg jdasfhasd89fa 2011.06.28. k ydsfgsdgsdg dsfgdsfzfszgh 2011.06.29. sze ds9fgisdfgsdfg asdfasdfasddf 2011.06.30. ...
6
votes
1answer
975 views

Why isn't sed greedy in this simple case?

$echo "foo 65 bar" | sed -n -e 's/.*\([0-9]\+\).*/\1/p' 5 Why is the output not 65? Shouldn't sed greedily match the [0-9]\+ part? How do I tell sed to match all of 65?
2
votes
3answers
703 views

sed, grep, or tr command that returns only Latin characters from a UTF-8 file

I am working with the text of the 300 tang poems, which unfortunately is a single file containing both Chinese and English. Since I am interested in 'extracting' the English, I hope to use sed, ...
9
votes
3answers
5k views

Delete the first n bytes of files

I've got an extreme problem, and all of the solutions I can imagine are complicated. According to my UNIX/Linux experience there must be an easy way. I want to delete the first 31 bytes of each file ...
4
votes
3answers
245 views

sed one-liner to replace word-medial capitals

I used OCR to turn some scans into plaintext, but unfortunately the letters 'fi' which are commonly joined in some fonts, got read in as capital W's. Now I need to replace all the W's with 'fi', and ...
9
votes
2answers
2k 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 ...
1
vote
2answers
4k views

sed command to replace a blank line with two lines of content

How do I replace the first blank line with two lines of content? I did see a question on replacing multiple blank lines with a single blank line in vim sed but don't quite see how to adapt that. So, ...
11
votes
6answers
5k views

How can you combine all lines that end with a backslash character?

Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash? For example, given the file: foo bar \ bash \ baz dude \ happy ...
2
votes
3answers
1k views

sed one-liner to delete any line that begins with a digit

Pretend I am working with this file. The beginning of the first visible line is obscured, so ignore that. 43 is the beginning of the next line, and that line would be deleted. The next line (which ...
2
votes
3answers
944 views

Delete files if contents do not match pattern

There are hundreds of .html files in a directory. I need to delete files that don't contain word bluecar in their contents. I thought sed with rm would do that, but I don't know how to combine them.
5
votes
4answers
974 views

How to append end of every line with the line itself

This is in file.txt: redcar bluecar greencar Im looking for ways to make it become: redcar redcar bluecar bluecar greencar greencar I've tried many ways using sed with no luck
0
votes
1answer
151 views

sed regexp for: [111]

INPUT RANDOMSTRING BLA-BLA [111] RANDOMSTRING BLA-BLA OUTPUT RANDOMSTRING BLA-BLA [200] RANDOMSTRING BLA-BLA [111] could contain various numbers with 3 digits: 120 or 300 or 400 or 101, etc. So ...
11
votes
4answers
2k 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?
2
votes
2answers
320 views

sed: get 2 lines from a single one

I have a list of filenames as id-datetime.txt, one each line, where id is always the same and datetime is ordered. I need first and last datetime so having a sed_script in a variable $script that ...
2
votes
2answers
200 views

Want to rsync some files after they've been sedded

I would like to rsync some HTML files to a webserver that are in production form. To get them in production form I have to trivially insert a ".production" string in a script tag. I can do this with ...
3
votes
2answers
172 views

Why isn't this sed regex matching?

I've recently started learning sed. I did $seq 10 | sed '/[^049]/d' I was expecting 4 9 10 as output. But I got 4 9 Where am I making mistake in understanding this regex?
2
votes
1answer
548 views

Sed: how to replace nextline \n symbol in text files?

I need to fix an error and to replace the second tag </time> with ` in an XML file with the following structure: <time>20260664</time> <tags>substancesummit ss</time> ...
1
vote
1answer
516 views

How to convert <a href=“http://xy.com”>XY</a> to [http://xy.com XY]?

Just for editing Wiki's.. So i have this input [from a larget text]: TEXT TEXT BLA-BLA RANDOMSTRINGS BLA-BLA TEXT TEXT BLA-BLA RANDOMSTRINGS BLA-BLA TEXT TEXT <a ...
3
votes
4answers
1k views

Replacing text from a list of replacements. Added complication: backslashes

I have a file A that contains pairs of strings, one per line: \old1 \new1 \old2 \new2 ..... I would like to iterate over file A, and for each line perform the replacement (e.g. "\old1 -> \new1") ...