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

learn more… | top users | synonyms

3
votes
5answers
158 views

How to replace only the third occurrence of the file in unix using only sed command?

How to replace the third occurrence of the string in the file using sed command. Example: Change only the third occurrence of is to us in the file. My input file contains: hai this is linux. hai ...
0
votes
0answers
20 views

sed: read whole file into pattern space [on hold]

Reading a whole file into pattern space is useful for substituting newlines, &c. and there are many instances suggesting the following: sed ':a;N;$!ba; [commands...]' However, it fails if the ...
1
vote
2answers
47 views

Copy the first 2 lines from a file and then remove the lines

I need to process a file using a shell script. The file arrives with the data as well as some embedded information releavant to how it is processed. The file is in the following format; ...
1
vote
1answer
32 views

split line based on space and delete the second part

I have a big file >fid|29290408|locus|VBIEntCas2262_0001| Phosphoglycolate phosphatase (EC 3.1.3.18) [Enterococcus casseliflavus EC20] ...
3
votes
4answers
2k views

Removing unwanted lines in text file

All I need is the sed or awk code to remove the even numbered lines in a text file. Nothing fancy I just need to remove irrelevant data on the even lines of a text file.
1
vote
2answers
29 views

Why does regex with \\$\{ work with egrep, but not with sed?

Given a text like this ./RFF_09 -f${FILE} -c${COND} inside a file, this egrep command will correctly match: egrep './RFF(.*) (.*)-c\\$\{COND\}' file but this sed command will not sed -n ...
1
vote
3answers
46 views

Removing mulitple lines

I am trying to write up a command line interfaces that will removes a particular section / lines of codes within a list of json files. By the way, the json file are located within the sub-folders of ...
2
votes
3answers
76 views

Adding text to beginning of text file

I am trying to add a line of text to the beginning of a text file (Actually not text, but just two backslashes). I am trying the following, which I found in this answer: Add lines to the beginning and ...
3
votes
7answers
220 views

can we print the last word of each line in linux using sed command?

suppose , if there is a file consisting of following lines , if they are 12345 567 7878 66 er3 t45t y6y46y 4y6 y656y y5y 46y6 65y7 y66uyuy yy46y6y The output has to look like: 66 ...
0
votes
2answers
55 views

How to convert Log file to CSV using bash

Is it possible to turn this log entry into a properly formatted csv file using sed/awk? I've tried multiple ways to do this using sed/awk. Alas I couldn't do it. Before I do it in perl. is this ...
3
votes
3answers
75 views

extract from line to line and then save to separate file

I tried my luck with grep and sed but somehow I don't manage to get it right. I have a log file which is about 8 GB in size. I need to analyze a 15 minute time period of suspicious activity. I ...
0
votes
1answer
13 views

Insert space at specified position in a file

I have this sample file: 234234|111||J1X0H8|SERP||R|D|666|M 234235|222||J1X0N5|SERP||C||555|M For each line, I need to insert a space after the 3rd character of the 4th column, like so: ...
10
votes
4answers
5k views

How to insert a new line after every second line?

sed/bash/wharever :D INPUT: sadf asdf yxcv cxv eqrt asdf OUTPUT: sadf asdf yxcv cxv eqrt asdf
2
votes
3answers
60 views

Replace multiline string in files

I have a number of files I want to update by replacing one multi-line string with another multi-line string. Something along the lines of: * Some text, * something else * another thing And I want ...
2
votes
4answers
54 views

Removing an empty line and the line above it

I have a large .fna file containing millions of DNA sequences. Due to some write errors after truncating my primers, I'm left with some sequence headers with no sequence following it. An example is ...
3
votes
2answers
722 views

Split log file by time range

I have a log file and would like to cut it into copies of 15min. In the log file the lines start with: dd mmm yyyy hh:mm:ss,xxx e.g. 12 Feb 2013 16:05:02,xxx log text... and the file rotates ...
33
votes
3answers
60k 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 ...
3
votes
3answers
144 views

sed fails to remove newline character

I've been using sed for quite some time but here is a quirk I came around with, which I am not able to resolve. Let me explain my problem with the actual case. Scene#1 printf "ls" | xclip ...
0
votes
3answers
52 views

how to replace a character within in a multiple double quoted words within a line using sed?

I have text file including text format like this. 012345,"[ThinkPadT2/3Gband,Mac]",Lenovo,"iPhone3G,A1241" How do i replace comma inside words only within double quote by pipe(|) operator using ...
4
votes
2answers
107 views

sed “e” and “g” flags not working together

Given this: echo AAA | sed -r 's/A/echo B/ge' I get this: Becho Becho B I would have thought I would get "BBB". This is with GNU sed version 4.2.1. What is going on, and how can I use the ...
4
votes
3answers
42 views

How to select first occurrence between two patterns including them

How can I select first occurrence between two patterns including them. Preferably using sed or awk. I have: text something P1 something content1 content2 something P2 something text something P1 ...
0
votes
0answers
13 views

Escape ampersand in bash variable used as a sed argument [duplicate]

I have a template Hello NAME welcome and want to replace NAME with data like "Judy&Mary". Why does this not work? TEMPLATE="Hello NAME welcome" DATA="Judy&Mary" echo $TEMPLATE | sed -e ...
1
vote
1answer
38 views

How can I use regex to extract the text following two conditions into a variable?

I'd like to extract a setting for a given host in the ssh_config file and put it into a variable. Host mysite HostName 123.1.1.1 User myuser Port 13245 GSSAPIAuthentication no ...
1
vote
2answers
47 views

Formatting ifconfig using sed/awk

I'm using ifconfig on OpenSUSE. When I run ifconfig eth0 I get eth0 Link encap:Ethernet HWaddr CE:FD:75:DF:A5:6D inet addr:172.16.4.177 Bcast:172.16.5.255 Mask:255.255.254.0 inet6 ...
1
vote
4answers
41 views

Two operations with sed on the same pattern

I use this following sed command for display names of files with a specific form: ls -1 *|sed 's/^\(.*\).png/\1\/\1,/g' If I have two files named BOH_Contour.png and BOV_Web.png, I obtain ...
0
votes
2answers
33 views

Replace some text with sed

I have tried this to replace the resis_test by m.gds by searching the key word LAYOUT_PRIMARY. sed -e 's/\(LAYOUT PRIMARY\).*\1 /m.gds/' < FILE.ex > b FILE.ex LAYOUT PRIMARY "resis_test" ...
2
votes
3answers
91 views

How can I insert the contents of a file into a string in bash

I would like to append the contents of a multi-line text file after a particular line in a string. For example, if the file file.txt contains line 1 line 2 I'd like do so something like printf ...
2
votes
2answers
186 views

How to remove all CRLF in file (not replace with LF)

I'd like to remove all carriage returns followed by line feeds (CRLF), such as \r\n in a file. How can I do that? I can't use dos2unix because that replaces CRLF with LF. And I can't use tr because ...
3
votes
4answers
119 views

How to swap entire html blocks in multiple files with sed

I have tons of html pages with a simlar content as follows: <div class="wrapper"> <div class="aaa"> ...lot of html1 here like div's/spans etc.. </div> <!-- end aaa --> ...
35
votes
2answers
50k views

How can I replace a string in a file(s)?

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub ...
1
vote
0answers
40 views

sed stdin behaviour

Using sed for a direct stdin treatment of data, I expect that a line is treated after the enter. This is what happens when I test on CygWin or Linux but on my AIX, a second new line is needed to treat ...
1
vote
2answers
40 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 ...
1
vote
5answers
61 views

Using sed to cut a string starting with n spaces and ending with n spaces

How to use sed to cut text starting with x spaces and ending with y spaces? For instance This is my string: kkk 111 fff aaabbb 5d98 ccc mmmppp 9369d and I want to get this output: ...
5
votes
5answers
267 views

How to run a command only if sed replace strings

I want to enable multiverse repository in Ubuntu by a command line. sed -i "/^# deb.*multiverse/ s/^# //" /etc/apt/sources.list && apt-get update This command will run apt-get update every ...
3
votes
3answers
132 views

Strip off word pattern at the end of string using sed

I have following string: ksh -x SCRIPT2.ksh $BUSSINESSDATE & PID=$!; ksh -x SCRIPT1.ksh $BUSSINESSDATE & PID=$!; I want to strip off the last word & PID=$!;. Desired result is: ksh -x ...
1
vote
6answers
5k views

Extract substring using regular expression on a Unix file

I have file with below contents . /ABC/RTE/AD_900_VOP_123/OPP /ABC/RTE/TRE/AD_900_VOP_145/BBB /ABC/RTE/AN_900_VFP_124/FBF /ABC/RTE/HD_900_FOP_153/WEW /ABD/RDV/AD_900_VOP_123/OPP ...
2
votes
2answers
66 views

Search and Replace question

I need to find all occurrences of AAsomeArbitraryStringBB and replace it with CCsomeArbitraryStringDD. So AAHelloBB Text AAByeByeBB becomes CCHelloDD Text CCByeByeDD. It's important to note ...
2
votes
2answers
76 views

Extracting values from a file keyed by multiple keys

Consider a file with key=value pairs, and each key is optionally a concatenation of multiple keys. In other words, many keys can map to one value. The reason behind this is that each key is a ...
3
votes
1answer
38 views

Filter out inconsistent data blocks

I would like to filter out rows that has inconsistent geno values in line-part blocks. In case of duplicate consistent values , I would just keep the first row. As example, 'gf345 part1' has more ...
5
votes
8answers
5k views

Can sed remove 'double' newline characters?

I have a document with a lot of empty lines. How can I remove them when there are 2 or more together. I tried sed "s/\n\n//" file but it didn't work. No error.
1
vote
3answers
64 views

Pattern Search between specific lines and print line numbers

I have a file of 20 lines. I want to search for a string between the lines 10 and 15 and print the string along with the line numbers of the original file. I have tried sed -n 10,15p file | grep -n ...
1
vote
2answers
46 views

find and replace a value in a line of text

I have a text file which contain only 1 line of text 1314:Battery:1.90:50:30 I'm trying to figure out if is there anyway to do a grep and sed to change the value 50 and 30 to the following ...
0
votes
1answer
28 views

Prepending text read from a file on the top of another file [duplicate]

I want to add "void mainfunction()" on the top of a file. I am reading the actual name of the file from another file and the name of the main function from another file. I was using sed -i '1i void ...
2
votes
4answers
55 views

Rename mp3 file names to a pattern

I have a list of like names likes so: /A/B/C/D/E/F/filename_album1-003.mp3 its more like album\d+ I would like to change it to something like /A/B/C/D/E/F/filename_song-3.mp3 song is just a ...
0
votes
1answer
39 views

How can I replace string in a file?

I have the following line in a file, I would like to insert \N between two comma 20121128020001,2012-11-28 02:00:01.000,,,,,,,3.80,,,,, I tried to use seed sed -i 's/,,/,\\N,/g' Test.CSV Where I ...
2
votes
1answer
48 views

How to use variables in sed command

I'm trying to run sed -i "/$variable/c \$variable1' file.txt But it's not displaying any change in the file: file.txt. But the same command, with out the variables works fine: sed -i "/variable/c ...
3
votes
3answers
212 views

Need to insert single quotes in text file for use as SQL query using sed

I have created a line of text that looks like this: INSERT INTO radcheck(id, username, attribute, op, value) VALUES (,,00:23:32:c2:a9:e8,Auth-Type,:=,Accept); I want to use sed to make it look like ...
4
votes
5answers
9k views

removing the first and the last character of every line from command line

I am trying to remove the first and the last characters of everyline in a text file and save the resulting truncated version in a new file. Does anyone have an idea about how to do that efficiently ...
2
votes
3answers
621 views

Reverse match in sed, replace opposite of what was found

Say I have this output from my syslog, precalculated src_hash => 20 bytes @ 0x7f811c001e20 A6 2D E5 CD 2A BA F0 42 56 66 19 D4 61 1A E3 A0 .-..*..BVf..a... C7 5E 5F 77 ...
3
votes
4answers
2k 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") ...