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

learn more… | top users | synonyms

2
votes
3answers
2k views

AIX's sed - in place editing

Is there any undocumented flag in AIX's sed implementation that allows in-place editing? The man page shows no flag for this operation, which is one of the most useful ones in other UNIX flavours.
3
votes
4answers
88 views

How to delete a specific number in file?

I have a file: 1 2 5 6 8 0 0 0 0 1 2 5 6 8 0 0 0 0 1 2 5 6 8 0 0 0 0 1 2 5 6 8 0 0 0 0 I want delete all zeros: 1 2 5 6 8 1 2 5 6 8 1 2 5 6 8 1 2 5 6 8 I tried tr -d [:0:]. Unfortunately, it ...
0
votes
1answer
30 views

How to replace word in specific column if it does not match an expression?

I have the following file: chr1 157784 157887 U6 0 - chr1 564813 564881 miRNA 0 + chr1 564879 564950 tRNA 0 - chr1 564952 565019 tRNA 0 + chr1 566062 566129 ...
11
votes
4answers
24k 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 ...
88
votes
4answers
136k 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 ...
0
votes
3answers
34 views

Extract Strings from Web Log file

Given a file containing web access logs for a YouTube video, every line is a hit and is in the format. 62.172.72.131 - - [02/Jan/2003:02:06:41 -0700] "GET /random/html/riaa_hacked/ HTTP/1.0" 200 ...
6
votes
3answers
229 views

How can I display 3 lines of text at a time onto 1 line?

I have a log file that displays data 3 lines at a time. Like this: 1 data 2 data 3 data 1 data 2 data 3 data 1 data 2 data 3 data I'd like to take each 3 lines and ...
3
votes
2answers
119 views

How to extract several data from a file and store it in a csv file?

I have a file in the following format : 19-08-02 Name appel ok hope local merge (mk) juin nov sept ...
3
votes
2answers
36 views

Using literal empty curly braces {} inside sed command from find -exec

I'm wondering if it is possible to include empty curly braces {} inside a sed replacement called from a find -exec. An example: find "$dir" -type f -name "*" -exec sed -i s/hello{}a/hello{}b/g '{}' ...
0
votes
3answers
44 views

Increase integer in string by 1 for each line

with for example sed, awk or python. there will be 548 lines in the original txt file from this: http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3 ...
19
votes
4answers
39k views

Delete last line from the file

I use sed to quickly delete lines with specific position as sed '1d' sed '5d' But, what if I want to delete the last line of the file and I don't know the count of lines (I know I can get that ...
0
votes
1answer
31 views

pattern regex for apache rewrite

I have a bunch of URLs counting to thousands i need to redirect and there are thre main patterns in URL first pattern There is a dash - starting the title url and want to remove that dash e..g ...
74
votes
8answers
107k views

How can I use sed to replace a multi-line string?

I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example: $ cat > alpha.txt This is a test Please do not be alarmed $ sed -i'.original' 's/a test\nPlease ...
3
votes
4answers
55 views

Bash converting path names for sed so they escape

I'm having a problem with a script. It is meant to change a value in a file called %DIR% so that it becomes a path name. The problem is the slashes in the directory name upset sed, so I get weird ...
0
votes
3answers
49 views

sed backreference: get each line and append it to end of line

I have a file with following content: $ cat -n w 1 Smith John 12345 jsmith 2 Jones Robert 23456 rjones 3 Green Ralph 34567 rgreen 4 Brown Fred 45678 ...
1
vote
1answer
27 views

Busybox ASH inside initrd has a problem when piping tee to sed

I'm trying to log the early boot output from the init script. This is what is working: mkdir -p /run/log && \ mkdir -p /tmp && \ mkfifo /tmp/stage-1-init.log.fifo1 && \ mkfifo ...
0
votes
3answers
69 views

Count all lines between a delimiter

I want to count the number of addresses attached to each device in the below example. Preferably using awk or sed. /dev/1 addr1 sometext sometext sometext addr2 sometext sometext sometext addr3 ...
1
vote
2answers
42 views

sed + remove char after specific word

I want to delete the last "." only if "." exist after "usa" word my sed command only removed the last "." , but what need to add to the sed syntax (a rule) in order to remove the "." only after ...
1
vote
1answer
42 views

why this simple sed script didn't work “nginx -V 2>&1 | sed -r 's/--/\\n/g'”

the output of "nginx -V" is a mess. nginx version: nginx/1.9.3 (Ubuntu) built with OpenSSL 1.0.2d 9 Jul 2015 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fPIE ...
2
votes
2answers
62 views

Print lines of a file between two matching patterns [duplicate]

How can I print all the lines between two lines starting with one pattern for the first line and ending with another pattern for the last line? Update I guess it was a mistake to mention that this ...
0
votes
2answers
43 views

Shell output from one command as input to other

I am trying to automate Forever JS to keep my dev server running and for that I need to check and stop the forever process to start another one. By running sudo forever list I get the following ...
-2
votes
1answer
63 views

How to find the word before a match

I have the following log file. I need to know if there are any fault / suspect condition using a shell script. I need to find fault and check the previous word, if it is greater than 0 then there is ...
0
votes
1answer
70 views

parse HTML in Bash (when grep just isn't enough)

This is driving me insane, as I'm sure there must be some program doing the job but I just can't find it. To be noted, I'd be on ARM/Raspberry so unfortunately Xidel is not an option here, unless ...
3
votes
2answers
77 views

Find each line matching a pattern but print only the line above it

I need to find a string and need to print the line above it. case1: The same line won't have the more than one matching pattern ie) consider a file containing $cat > para returns between the ...
4
votes
3answers
373 views

How to replace a multi line code with sed?

I have a large file which has special characters in it. There is a multi line code there, that I want to replace with sed. This: text = "\ ------ ...
-3
votes
4answers
50 views

How to print only the ticket numbers?

Following is the content of the file and i need to print only the ticket numbers which are enclosed in "[]". Curious to know if this can be done using both sed and grep commands. ...
-2
votes
1answer
18 views

Remove some elements of a string and replace with others

How can I convert this : RS.PK.03.01.4200.03.014.01 to man03140101? What needs to be done? Text before 5th "." has to be removed : RS.PK.03.01.4200. The remaining text should be converted ...
-1
votes
3answers
32 views

How to delete the first few lines of data and a specific matching numeric number until the very end of data

51 0 0 5 0 0.0 0.0 0 1 2 3 4 5 6 7 8 1, 2 0.998 0.567 3, 2 Rs12345 0.7 0.2 3, 2 Rs31256 0.56 0.311 3, 2 Rs25691 0 0 012.1313010310 0.1213212 0.2121331321 0.0121654564 0.254564564 ...
3
votes
1answer
47 views

Replacing a & in XML correctly?

I want to replace a '&' with &amp; Except the following occurance of XML special characters. " &quot; ' &apos; < &lt; > &gt; & &amp; I tried the ...
17
votes
5answers
21k views

Convert file contents to lower case

I have temp file with some lower-case and upper-case contents. Input Contents of my temp file: hi Jigar GANDHI jiga I want to convert all upper to lower. Command I tried the following command: ...
3
votes
1answer
88 views

Print some lines before and after pattern match excluding lines matching another patterns

I use awk on Solaris to print 5 lines before and after a pattern match. Currently, the following one-liner does the job: /usr/xpg4/bin/awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print ...
3
votes
2answers
54 views

How can I do a binary search of a text file for a specific character string?

I have a text file that contains a large number of records, each on a single line. Some of the records have special characters that have been corrupted, and I'm attempting to find those by looking for ...
3
votes
2answers
226 views

sed capture groups not working

I have a string of the format [0-9]+\.[0-9]+\.[0-9]. I need to extract the first, second, and third numbers separately. As I understand it, capture groups should be capable of this. I should be able ...
-1
votes
1answer
37 views

Extract value from XML

I have this XML: <results><testsuites><testcase name="XXXX (XXXXX)" time="20.3817"><result>success</result></testcase><total_time value="20.3830" ...
4
votes
3answers
796 views

How to 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. ...
2
votes
2answers
784 views

Get current timezone on Centos 7

On previous version of centos , i used the following command to get the current timezone in my bash script timezone=$(sed 's/ZONE=//g' /etc/sysconfig/clock) Output is "America/New_York" How can i ...
3
votes
3answers
844 views

How to add new lines from another file with sed

I'm trying to find the sed command so that I can put filename into filename1. These are my two separate files. INPUT filename has: Cindy 11 22 54 Chester 48 12 84 INPUT filename1 has: Name ...
0
votes
1answer
24 views

Using sed command change a string on a certain line with a certain string

Basically what I need help with is, I need to use sed to change a string in a file only on the 4th line if it contains a string called "test" So basically What I got right now is: sed '/test/ ...
0
votes
1answer
33 views

sed: -e expression #1, char 44: unterminated 's' command [closed]

I am aware that this type of question has been asked by many people. However, I am a beginner in Linux so I hope you can please help me this line of code: sed -i ...
-1
votes
2answers
51 views

What does sed 's/5//g' mean in this command line?

Here is output of df -h. [root@ip-10-0-7-225 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 8.0G 1.5G 6.6G 18% / devtmpfs 478M 0 478M 0% /dev tmpfs ...
2
votes
2answers
113 views

Replace text quickly in very large file

I have 25GB text file that needs a string replaced on only a few lines. I can use sed successfully but it takes a really long time to run. sed -i 's|old text|new text|g' gigantic_file.sql Is there ...
1
vote
1answer
27 views

insert string for missing value

I've run into some problems with editing a text file I've received due to some missing values in a specific column for some rows. For example, here is what the file looks like (mostly): ID10 dad ...
1
vote
2answers
23 views

Strip certain objects out of a flat file database

I have a flat file database and I need to strip certain objects from it. There are numerous blocks in the file but I am interested in 2 objects in the OBJECT NickCore block. Example below: OBJECT ...
0
votes
1answer
42 views

How do I remove a line if it begins with a . or a > using sed?

For the life of me I can't get this to work properly (forgive me if this is a trivial question). I have a text file that have lines that begin with either a period, ".", or a right arrow, ">". I need ...
1
vote
2answers
46 views

Change a string with sed

After using a filter to get a list of process like the next one: FOO_AAAAA_00001 FOO_BBBBB_00001 FOO_ABABAB_0000 FOO_ABCCDAA_00 FOO_12345678_0 FOO_JJJ_00001 FOO_VLLLL_00001 FOO_KKAKAA_00001 The ...
1
vote
3answers
103 views

sed to remove all after a string?

I'm trying to use sed to remove the rest of a line after: HTTP1.1" 200 I can't figure out how to get sed to understand I want that whole thing as a string to match, including the double quote and ...
1
vote
1answer
32 views

How do I get an acceptable last modified timestamp for my Jekyll site using a shell script?

I would like to be able to provide last modified timestamps to my Jekyll posts and pages using a shell script. The problem is that I do not know how to automatically generate an acceptable timestamp. ...
9
votes
6answers
41k 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 ...
0
votes
2answers
31 views

sed + add comment to IP - only if IP is matched exactly

my target is to add "#" to the IP's with sed, But Only if IP is matched exactly!!! for example: sed 's/72.16.90.12/#72.16.90.12/g' /etc/hosts more /etc/hosts 72.16.90.121 host1 ...
1
vote
1answer
37 views

sed + update 4 different IP's in hosts file in one sed command

how to run the follwoing sed command in one command line? sed -i s'/12.1.0.11/#12.1.0.11/g' /etc/hosts sed -i s'/12.1.0.12/#12.1.0.12/g' /etc/hosts sed -i s'/12.1.0.21/#12.1.0.21/g' /etc/hosts ...