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

learn more… | top users | synonyms

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. ...
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 ...
3
votes
1answer
747 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
950 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
988 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 ...
6
votes
2answers
417 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
441 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
178 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 ...
-6
votes
5answers
300 views

Manipulating a file with sed

I have a file called students.txt and it contains the following data in the format Surname, Forename: day.month.year: Degree: Smith, John: 15.01.1986: MSc IT Taylor, Susan: 04.05.1987: MSc IT ...
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 ...
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 ...
6
votes
1answer
980 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?
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
246 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 ...
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, ...
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
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 ...
5
votes
4answers
978 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 ...
2
votes
2answers
202 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?
53
votes
6answers
13k views

Is there a basic tutorial for grep, awk and sed?

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, ...
2
votes
1answer
552 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> ...
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") ...
1
vote
1answer
517 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 ...
6
votes
2answers
3k views

sed one-liner to delete any line that does not contain lowercase letters

So, basically THIS LINE WOULD BE DELETED and (THIS LINE WOULD ALSO BE DELETED) but Indeed, THIS LINE WOULD NOT
1
vote
3answers
332 views

Switch from grep to sed

I have a files students.txt, with lines of the form: Surname, Forename: Day.Month.Year: Degree For example: Smith, John: 15.01.1986: MSc IT Taylor, Susan: 04.05.1987: MSc IT Thomas, Steve: ...
4
votes
1answer
256 views

sed : insert sth before subsequent lines that begin the same, but are not the same

I have a LaTeX file with one glossary entry per line: ... \newglossaryentry{ajahn}{name=Ajahn,description={\textit{(Thai)} From the Pali \textit{achariya}, a Buddhist monk's preceptor: `teacher'; ...
3
votes
4answers
259 views

Remove empty configuration section

Files like ~/.config/vlc/vlcrc are 99% junk if you want to version control only the configuration options. I've got a script to remove the comments, but there's a ton of empty configuration sections ...
2
votes
2answers
307 views

non-case-sensitive sed - OpenWrt

the input would be like this in a file: <sCripT and the parsing looks like this: sed -i "s/<script//g" SOMETHING.html the "/gi" is not good, because OpenWrt doesn't knows it (busybox..): ...
8
votes
3answers
3k views

Zero-fill numbers to 2 digits with sed

Input: 201103 1 /mnt/hdd/PUB/SOMETHING 201102 7 /mnt/hdd/PUB/SOMETH ING 201103 11 /mnt/hdd/PUB/SO METHING 201104 3 /mnt/hdd/PUB/SOMET HING 201106 1 /mnt/hdd/PUB/SOMETHI NG Desired output: 201103 ...
2
votes
3answers
714 views

Filtering paths to a specific depth

I'm using rsync and the flags -nPaAXz ~/ to check which files are going to be copied. This is far too verbose to make any sense of. How could I filter the output so I view the list of files/folders ...
3
votes
2answers
2k views

sed or tr one-liner to delete all numeric digits

So, I have this textfile, and it consists of mostly alphanumeric characters. It's a standard document. But since I copied it and pasted it from a PDF, there are page numbers in there. I don't much ...
2
votes
3answers
3k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
6
votes
3answers
215 views

Locate postion then make a change using sed

This script uses sed to change all "" to "new stuff". How would one change just the "" after the yyy: using sed or anything else? cat >sample.txt <<EOF xxx: "" yyy: "" } EOF sed ...
7
votes
4answers
9k views

Copy/rename multiple files using regular expression (shell script)

I have a collection of files matching a pattern such as 'assignment02.cc', 'assignment02.h', 'assignment02.txt', etc. I would like to copy/rename these files into 'assignment03.cc', 'assignment03.h', ...
9
votes
2answers
3k views

How to remove multiple lines per occurrence in a file?

Say I have this 857835 line file, containing stuff like this: a1 rubbish1 rubbish2 rubbish3 rubbish4 a1 rubbish5 rubbish6 rubbish7 rubbish8 And I wish to remove all occurences of a1 and the next ...
1
vote
4answers
2k views

Processing control characters

I have a log file which contains a bunch of non visible control characters such as hex \u0003. I would like to replace this using something like SED, but can't get the first part of the regex to ...
5
votes
4answers
224 views

How could I simplify this command to only use awk?

awk '/user/ {print $1 }' /etc/userdomains | sed 's/://' the format of /etc/userdomains is domain.tld: user otherdomain.tld: otheruser
3
votes
3answers
2k views

Substitute part of text file using bash script

I'm writing a shell script (bash) to fetch and build several bits of software. The script also writes several small config files and needs to alter a couple of pre-existing config files. What is the ...
1
vote
3answers
223 views

Directory filenames with sed with whitespaces

I'm working on a script and I'm stuck, even with the help of teh googles. Here's my code: for FOLDER in `find . -type d | sed "s#^.#$(pwd)#" | sed 's/ /\ /g'` do echo "$FOLDER" done This will ...
8
votes
4answers
7k views

grep — removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...