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

learn more… | top users | synonyms

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 ...
4
votes
2answers
672 views

Append line to many files

I want to add some text to over 200,000 files I am trying this find . -name *.txt -print | xargs -I % echo "hello world" >> % But nothing is happening. When i run find . -name *.txt it work ...
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 ...
1
vote
3answers
3k views

sed pattern matching

I recently asked someone at work about how to take the output of ipcs -qa and make it space delimited, so I can parse it/store it in the database for monitoring. He gave me this: ipcs -qa | sed 's/ [ ...
1
vote
1answer
87 views

What happens for the last line when using N command with sed

Here is my sed command: echo -e "AB\nCD\nEF\nGH" | sed 'N; D;' It prints: GH while sed processing input line by line, what happens with the last line? (When after that sed saw End-Of-File)? I ...
4
votes
1answer
322 views

substituting third occurrence of a pattern using sed

Here is a sed command. I want to substitute third occurrence of 00 to 11 echo "0000000000000000" | sed -e 's/00/11/3' The output is: 0000110000000000 But I thought output would be: ...
2
votes
2answers
2k views

regex replace text in xml file within node from the command line

I have an XML file and I would like to replace everything that is between the open and closing tag within multiple instances of the g:gtin node with nothing. Is this possible from the command line, ...
24
votes
9answers
6k views

Addition with 'sed'

I am trying to perform a mathematical operation with sed, but it continues to treat my variables as strings. The input is of this kind: $ echo 12 | sed 's/[0-9]*/&+3/' $ 12+3 I'd like to have ...
2
votes
1answer
230 views

Is it a good idea to supplement shell script with perl purely for use of regex?

I'm looking for a simple way to utilize regex in a UNIX shell script where not every system will have perl extensions built into grep. What is really helpful about perl regex here is back/forward ...
4
votes
1answer
4k views

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 ...
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?
3
votes
2answers
98 views

What is the usage of pattern before substitute command in sed

There is an example in this link about sed: To delete the first number on all lines that start with a "#" use: sed '/^#/ s/[0-9][0-9]*//' What is the benefit of first pattern(/^#/)? It could be ...
9
votes
3answers
2k views

Substituting strings in a very large file

I have a very long series of urls with no separating character, in the same format as below: http://example.comhttp://example.nethttp://example.orghttp://etc... I want each URL to be on a new line. ...
4
votes
3answers
575 views

How to skip file in sed if it contains regex?

I currently use the following simplified command to remove trailing whitespace and add a newline at end of file where needed: find . -type f -exec sed -i -e 's/[ \t]\+\(\r\?\)$/\1/;$a\' {} \+ As ...
1
vote
2answers
135 views

sed substitution matches too many inputs

This is my command: echo "Test" | sed -f <(sed -e 's/.*/s,&,gI/' mydic) The file mydic contains 2 columns delimited by commas (,) a,AlphabetA . . . e,AlphabetE . . ...
4
votes
1answer
4k views

Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

Is it possible to do it in one liner? I have an output like this: "First line" - Description " Second line" - Description "Third line" - Description " Fourth line" - Description This input ...
0
votes
2answers
503 views

Removing a long string from php files - using grep and sed?

I have encountered some malware on my Linux server, and am trying to remove it from many php files. I've tried endlessly with grep | sed and grep | tr and couldn't even erase a simple text string, ...
2
votes
1answer
2k views

Variable substitution into a sed script

After running this code #!/bin/bash rm -f myfile myfile-individual echo "Greetings, foobar!" > myfile MYSTRING="Jack" cat myfile | sed 's:foobar:$MYSTRING:' > myfile-individual we end up with ...
2
votes
2answers
601 views

Search and replace with sed

Last week I accidently externalized all my strings of my eclipse project. I need to revert this and my only hope is sed. I tried to create scripts but failed pathetically because I'm new with sed and ...
1
vote
2answers
950 views

How can I delete lines that has an empty first column?

INPUT: CC2352345|m,safnlasndfmnyxdcvyxcvyxcv |klasjdf |3|lasdjflasdf| |2345567356 CC3543353|asdfasdffghntz |klasjdf |3|lsajdfl | ...
4
votes
2answers
692 views

Why does this awk command not play as well with find as sed does?

I am very much a newbie at Unix/Linux command line stuff, and mostly get to where I'm going by copying, pasting, and modifying code I find on the internet. Just mentioning that in hopes people will ...
2
votes
1answer
2k views

Swap two columns in a CSV using SED

I have a CSV file that contains 10 different fields (, is the deliminator). Example data: student-id,last,first,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2 pts-avail,,,100,150,100,200,150,100,300 ...
2
votes
1answer
3k views

Use AWK add new line if match exists

I have a file with the following format: aaa@ bbb bbb@ ccc& ddddd@ aaaf ccc@ ddd& eee: abcd& dassda The output should be: aaaa@ bbb bbb@ ccc& ddddd@ aaaf ccc@ ddd& eee@ ...
4
votes
4answers
991 views

Replace whole line in a file from command-line

I have a text file which has some contents similar to this: # General information about the project. project = u'Py6S' copyright = u'2012, Robin Wilson' # The version info for the project you're ...
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? ...
1
vote
6answers
438 views

Print word that doesn't contain character

My file is in the following format: this!,is!,another!,test,yes! this!,is!,another!,column,yes! no,not!,another!,column My output should be: test column no It shouldn't contain the '!' ...
3
votes
2answers
944 views

simple script rotating table

I'm trying to write a simple script, that just rotates csv table. I mean I have some file: head1;head2;head3 field11;field12;field13 and so on. All I want, just to make my file ...
2
votes
2answers
2k views

Recursively replace string in files

I want to replace the backslash in the string page_path\example_filename.txt with a forward slash. I also want to be able to run this on a large file system and have it recursively search all ...
3
votes
2answers
4k views

Sed: replacing digits

I am trying to write some install scripts for when I install/upgrade Ubuntu. I'm having trouble with fixing logrotate.d: /var/log/ufw.log { rotate 7 weekly missingok notifempty compress delaycompress ...
1
vote
2answers
973 views

To pass a parameter with sed

For my script I have to read the data from a file. For example I have 2 files: one is a text file, the second is a csv file with 10 columns. What I am trying to do is to read the csv and change the ...
4
votes
3answers
4k views

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/'
4
votes
2answers
19k views

Using sed to get rid of characters < > ,

I have a file with lines as follows: ... ... <230948203[234]>, ... ... <234[24]>, ... .. I would like to use sed to remove the characters < , and > from every line I tried using ...
4
votes
1answer
880 views

Use sed to fix broken newlines

I have a folder full of files, each with many hundreds of lines. Many of these files have occasional broken newlines: when I open them in vim, many have ^M^L interspersed where newlines should be. ...
28
votes
3answers
56k views

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 ...
1
vote
3answers
5k views

grep piping into sed, replacing inline; but I want sed to print the filename and changed line. Is it possible?

Here's my command (break intentional): grep FOO "/Users/gjtorikian/blah" -l | xargs sed -i '' '/FOO/{s/FOO/BAR/g; w /dev/stdout }' At the high-level: grep for FOO in the blah directory; pipe in ...
3
votes
2answers
1k views

Insert a multiline string into another string

I need to insert lines into an xml file : Insert <one> </one> into <tags> </tags> To obtain <tags> <one> </one> </tags> I tried this: ...
6
votes
2answers
20k views

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 != ...
3
votes
3answers
210 views

How to perform an action only on the first line?

sed 's/.\(.*\)/\1/' myfile Say myfile contains: abc def ghi I want to remove the first character only from the first line but the above removes it from all the line.
3
votes
5answers
668 views

changing pattern of a text file

A text file has contents something like chair table pen desk Now i want it to be changed and stored in a variable say var as below ('chair','chair'),('table','table'),('pen','pen'),('desk','desk') ...
3
votes
2answers
229 views

What is this sed command doing?

Can someone explain me what this sed command is trying to achieve? sed 's/^[[:space:]]*//;s/[[:space:]]*$// I understand that it is searching for whitespace characters from the beginning of the ...
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 ...
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 ...
1
vote
2answers
173 views

Where does the -i option in sed store the lines matching a regex?

I am trying to release space in my server's hd so I'm matching old dates in a log file with sed and it is decreasing but my root partition is now using 98% percent of it's space instead of 91% it was ...
4
votes
2answers
623 views

Replace one line with STDIN in a Makefile

One Makefile target is supposed to be in charge of: grabbing the output of $ perl Markdown.pl src/index.md (markdown to html) using that string to replace one line, something like CONTENT ...
1
vote
2answers
466 views

Prepend x/ to line where x is a variable on each line

I want to add $x/ from data formatted as $x-xyz (xyz is a mix of text and number to be ignored). Example data AB-10C CD-02E Output AB/AB-10C CD/CD-02E How do I describe the variable as text ...
3
votes
4answers
445 views

Align a hex text file at 9 bytes each line

I want to align the selected portion of a text file at 9 bytes each column. For example suppose my text file looks like below. 00 2f c6 b8 29 fd 02 37 11 00 9f 74 34 0b 60 72 38 20 00 9e 61 33 8e ...
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 ...
22
votes
10answers
6k views

How can I write to the second line of a file from the command line?

I have an external program that produces an output file (largish, 20K lines possible). I need to insert a new line between the existing line 1 and line 2. I've been looking at awk and sed - I use ...
6
votes
1answer
614 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 ...
4
votes
2answers
840 views

Moving average on a log file with awk or other unix utilities?

Scenario: I have a log file that has a few number of entry "classes", like this: R0 dx=0.00500 rb=0.00000 sn=1 3145.88 2.59 0.08 se=21315 id=16190 R0 dx=0.00300 rb=-1.00000 sn=1 3150.40 2.38 0.05 ...