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

learn more… | top users | synonyms

8
votes
2answers
4k views

Is there a way to detect null bytes (␀, NUL, \0) in sed?

Related to another question, in order to fuzzily detect binary files, is there a way to detect ␀ bytes in sed?
8
votes
2answers
1k views

Deleting all C comments with sed

I am trying to write a script that will delete all comments and everything in between inside C files in my current directory. I've been using sed, and this is what I have so far: sed -i '/ * [^()] ...
8
votes
3answers
5k 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
4answers
5k views

Remove files older than 5 days in UNIX (date in file name, not timestamp)

I want to delete log files which are older than 5 days from a directory. But deletion should not be based on the timestamp of file. It should be based on the name of file. For Example todays date is ...
7
votes
4answers
330 views

Output part of each line to a separate file

I have a file like this: a AGTACTTCCAGGAACGGTGCACTCTCC b ATGGATTTTTGGAGCAGGGAGATGGAATAGGAGCATGCTCCAT c ATATTAAATGGATTTTTGGAGCAGGGAGATGGAATAGGAGCATGCTCCATCCACTCCACAC d ...
7
votes
10answers
2k views

What's a good way to filter a text file to remove empty lines?

I have a .csv file (on a mac) that has a bunch of empty lines, e.g.: "1", "2", "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum ...
7
votes
3answers
1k views

How to remove a specific character in a string but only if there are no numbers in that line in Linux

I've been stumped with this seemingly simple-to-fix problem.. well, for a while. Here's example output from the file I need to edit: $cat file George Washington Geneva Convention 123,281,029 USD ...
7
votes
4answers
4k views

sed on OSX insert at a certain line

So I've been using 'sed' on linux for a while, but have had a bit of difficulty trying to use it on OSX since 'POSIX sed' and 'GNU sed' have so many little differences. Currently I'm struggling with ...
7
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 ...
7
votes
4answers
526 views

How to split long sed expression into multiple lines?

sed 's/[long1][long2]/[long3][long4]/' file.txt I would like to split this command onto multiple lines - f.x. something like this: sed 's/ [long1] [long2] / [long3] [long4] /' ...
7
votes
2answers
10k views

Extracting a regex matched with 'sed' without printing the surrounding characters

To all the 'sed' doctors out there: I have a seemingly trivial 'sed' question to which I have not been able to find a solution. How can you get 'sed' to exctract a regular expression it has matched ...
7
votes
4answers
9k views

Delete range of lines above pattern with sed (or awk)

I have the following code that will remove lines with the pattern banana and 2 lines after it: sed '/banana/I,+2 d' file So far, so good! But I need it to remove 2 lines before banana, but I can't ...
7
votes
8answers
10k views

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 ...
7
votes
1answer
5k views

Substitute text with sed and keep part of the original text

I am trying to convert <id>1</id> <Name>ENTERPRISE RESOURCE PLANNING</Name> to: <column name="id">1</column> <column name="Name">ENTERPRISE RESOURCE ...
7
votes
3answers
224 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
3answers
2k views

How can I instruct BSD sed to interpret escape sequences like \n and \t?

I have a sed replacement command that I would like to be compatible with BSD sed as well as GNU sed. Extended regular expressions are not an issue as I do not need them in this case. My primary ...
7
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?
7
votes
3answers
977 views

Print a line only if the next line does NOT contain a particular match

I am trying to search a log file for logged activities that did not complete. For example, I log a "Starting activity for ID 1234..." and if successful, the next line will be "Activity 1234 ...
7
votes
1answer
314 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 ...
7
votes
4answers
10k 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', ...
7
votes
1answer
676 views

How do you save a complex regex for multiple reuse in sed?

In using sed, I often create rather complicated and intricate regexes that I need to match twice in a file. Is there a way for me to save this regex and just reference it twice? Maybe something that ...
7
votes
1answer
1k views

Is there an option to make sed fail if pattern not found?

I need to find and replace some patterns in some files, but I need it to return 1 or something if a pattern is not found. Can I do this with sed alone or do I need to check whether the pattern exists ...
7
votes
0answers
220 views

regarding portable sed -e… d b or ! b?

In this edit Stéphane Chazelas POSIXifies (again) my sed formatting by inserting an -expression break and another -expression statement. Now, I might just ask him why in the comments, I suppose, but ...
6
votes
4answers
673 views

Using sed to add a URL to the start of every line

I have the URL (see below) of a certain web page that lists many different versions of a software package. URL=http://ftp.gnu.org/gnu/wget/ The following one-liner gets me the latest version tar ...
6
votes
5answers
254 views

Get filesystem name and usage with `df -h`

df -h | grep test | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: /test /test/drv0 /test/drv1 df -h | grep test | cut -d '%' -f1 | sed -e 's/*%.^ //g;s/.*[ ^I]//' Output: 10 20 15 I want to ...
6
votes
8answers
1k views

Grep rest of line…after match

I have a file containing only two lines, with the following structure: $ cat /tmp/pwpower.log 000D6F0000D34227, -114.10 000D6F0001A405C4, -130.09 The values are power values of my solar plant. ...
6
votes
5answers
514 views

What's wrong with this sed expression?

$ echo "104_Fri" | sed 's/^\([0-9]+\)_\([A-Za-z]+\)$/\1;\2/' 104_Fri I would like to match the digits at the beginning and the letters at the end - each as a group. Afterwards I want to output the ...
6
votes
4answers
10k views

Processing bash variable with sed

Been banging my head off a wall on this bash variable LATLNG contains a latitude & longitude value in brackets like so (53.3096,-6.28396) I want to parse these into a variable called LAT and ...
6
votes
5answers
244 views

Strategy to extract movies's name from this ununiform dataset?

I am working on a movie database problem to improve regular expressions, this is the problem I'm running into. My dataset looks like this: Movie Name (variable space and tabs) year Movie1(can ...
6
votes
4answers
4k views

How to remove multiple blank lines from a file?

I have some text-files I use to take notes in - just plain text, usually just using cat >> file. Occasionally I use a blank line or two (just return - the new-line character) to specify a new ...
6
votes
3answers
8k views

How to sed only that lines that contains given string?

INPUT: Select ASDF 325 sdfg sdflk lk Select TRG 46sdg rasdftz fsgs 45 Select ASDF 6ffg sdfg 4456 sdrg OUTPUT: Select ASDF 325 XXXX sdflk lk Select TRG 46sdg rasdftz fsgs 45 Select ASDF 6ffg XXXX ...
6
votes
4answers
505 views

how to remove ctrl F in unix file

While editing a unix file I'm getting data as below in vi editor. MGW:^FVMG107 MGW:^FVMG113 MGW:^FVMG108 MGW:^FVMG103 where in above data ^F is not viewable in cat command. I have tried dos2unix ...
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
6
votes
3answers
171 views

sed with multiple expression for in-place arguement

I am trying to replace multiple words in file by using sed -i #expression1 #expression2 file Something 123 item1 Something 456 item2 Something 768 item3 Something 353 item4 Output (Desired) ...
6
votes
3answers
1k views

Remove only the commas present within the double quotes

In a text file, I want to remove , (commas) and also the " (quotes) (only if the double quotes contains numbers seperated by commas). 56,72,"12,34,54",x,y,"foo,a,b,bar" Expected ouput ...
6
votes
5answers
20k views

Multiline pattern match using sed, awk or grep

Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and } So it should be able to match 1. {} 2. {.....} 3. ...
6
votes
4answers
4k views

How to extract logs between two time stamps

I want to extract all logs between two timestamps. Some lines may not have the timestamp, but I want those lines also. In short, I want every line that falls under two time stamps. My log structure ...
6
votes
5answers
1k views

resolve all ip addresses in command output using standard command line tools

I have several log files that contain a bunch of ip addresses. I would love to be able to pipe the data through a program that would match and resolve ip addresses. I.E. cat /var/log/somelogfile | ...
6
votes
3answers
7k views

How to insert a line into text document right before line containing some text in bash?

I have a variable say $strToInsert I have a file file.html I wonder how to find last appearence of </head> and insert a new line before line with it and fill it with $strToInsert contents? ...
6
votes
1answer
612 views

How to detect and delete lines containing ˆ@

I have a simple problem: In my file, the are lines containing the string ˆ@ˆ@ˆ@ˆ@ˆ@ˆ@. I just want to delete all lines with this string, using for example the sed or grep commands. And I would like ...
6
votes
1answer
256 views

How do I print all but the Nth to last line in sed?

I'd like to do the complement / "opposite" of sed 13q;d <file.txt More generally, is it possible to do this sort of complement/inverse/opposite in sed? Or only for regexes? How do I print all ...
6
votes
6answers
7k views

bash find lines starting with string

I have a bunch of files and I want to find which one contains sequential lines starting with a certain string. For example for the following file : Aaaaaaaaaaaa Baaaaaaaaaaa Cxxxxxxxxx Cyyyyyyyyy ...
6
votes
2answers
381 views

How to report “sed” in-place changes

When using sed to replace strings in-place, is there a way to make it report the changes it does (without relying on a diff of old and new files)? For instance, how can I change the command line ...
6
votes
1answer
161 views

Confusion about sed and '>'

The following command works fine: sed s/input/raw_input/ p.py >p2.py && mv p2.py p.py However, the following command turns p.py into an empty file: sed s/input/raw_input/ p.py >p.py ...
6
votes
4answers
14k 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 ...
6
votes
4answers
4k views

Why is sed giving me an error about an unterminated `s'?

I have a set of sed replacements in a bash script and am getting an error about an unterminated `s' command. Here's what the sed line looks like: sed -n -e "s/TMPFOO1/$FOO1/" -e "s/TMPFOO2/$FOO2/" ...
6
votes
2answers
3k views

Replacing missing value blank space with zero

I have input.txt tab-delimited text file around 30K lines, I would like to check each row (s1..s30K lines) for missing value (i.e blank space) and fill the missing value with zero value.See out.txt ...
6
votes
4answers
4k views

Concatenate lines by first column (AWK or SED)

How can I use AWK in the following situation? I want to concatenate lines that start with the same column. Only the first column is kept after the join (in this case aaa, www, hhh). The file may be ...
6
votes
2answers
642 views

How would I sort these directory names numerically?

I'm aware that I can somehow sort this output numerically (so cpu1/ follows cpu0/) ... I could probably get something to work eventually by splitting up the string various ways with awk, but is there ...
6
votes
1answer
1k 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?