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

learn more… | top users | synonyms

2
votes
2answers
8k views

How can I replace text after a specific word using sed?

I have a file named .ignore. In need to replace the projdir. For example: ignore \..* ignore README projdir Snake I need to replace Snake with, for example, "PacMan". I read the man page, but I ...
4
votes
2answers
7k views

Sed: Replace N first occurrences of a character

I am looking to replace the 5 first occurences of the whitespace character per line inside a sed script. Here's what I have so far sed -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" ...
-1
votes
2answers
4k views

Bash -Extract characters from string

Suppose I have the following variable $string = 'cat somecommandhere' Now the output of the above cat command is test="Thanks serverfault" I want to extract the value between the quotes and ...
0
votes
2answers
199 views

sed extract with multiple options in end pattern

I get some output from a command, I have to extract text between two patterns from the output. Start pattern is fixed. But the end format can be one of two (lets say word2-1 and word2-2). Is there ...
2
votes
1answer
67 views

extracting and copying parts of tex files into new tex files

I have a bunch of tex files in different folders on my system. The goal is to create new tex files (using a template, that has the title page, packages, new commands,...) that combines all the ...
2
votes
2answers
346 views

changing pattern by removing spaces?

file : hi hello,new york, , ,brazil site ,brazil january,month is feb , , ,indiana jones task: delete all horizontal space (spaces and tab) between any two comas (like , ,) if ...
2
votes
1answer
126 views

how do i suppress seds unnessary output while using option -e?

why n option is not working here [root@testgfs2 final]# echo a b c | sed -ne 's/[[:blank:]]/"/gp' -ne 's/^/"/pg' -ne 's/$/"/pg' a"b"c "a"b"c "a"b"c" i just need : "a"b"c" actually ...
5
votes
3answers
1k views

Multi-line replace

I spent ages trying to figure this out. As it happens, I don't need to do it any more; I found another way. But for the sake of my sanity, I'd like to know the answer anyway. It's quite easy to use ...
3
votes
2answers
5k views

Strip trailing whitespace from files

The answer to removing trailing whitespace with sed has most of the answer, but I want sed -i 's/[ \t]*$//' $1 to be able to take arbitrary number of file arguments as a shell script on the command ...
1
vote
4answers
86 views

How to find what device a file is on (and use that in a script)?

I want to find out what device my file is on so that I can use it in a script. I can get this far: $ df . Filesystem 512-blocks Used Available Capacity Mounted on /dev/disk0s2 498438976 ...
2
votes
4answers
115 views

How do I correct both too many and too few space characters?

I have files that are edited by people other than me. They contain lines that look like this: <E> This is text </E> I use some Bash shell scripts do a bunch of text replacement ...
1
vote
1answer
209 views

Why is it that , to newline is working but newline to , is not working sed?

I have got a text file with a list of first names, and I am trying to replace newline with ,. Here is the command I am using sed 's/\ /,/g' file while the reverse is working sed 's/,/\ /g' file ...
1
vote
2answers
146 views

sed pattern matching with deletion over a line

I want to delete only some spaces(say 4) after the line$#*425 with space I am not able to complete it please suggest echo "line$#*425 with space " | sed 's/ /\1 \2/g' i am thinking of ...
1
vote
2answers
675 views

Print fields from file only if line starts with a specific word

I have the following file: … LINK=dummy 172.17.100 => 10.218.11 [PATH=/etc] FILE=hosts 172.17.100 => 10.218.11 [PATH=/etc] FILE=network 172.17.100 => 10.218.11 ...
0
votes
2answers
394 views

remove a char string from a command output (sed, or maybe something else)

I'm trying to retrieve the distro name from the lsb_release -i output, but using lsb_release -i | sed 's/Distributor ID: //g' won't do the trick. What am I doing wrong? Or maybe should I change ...
1
vote
1answer
2k views

How to calculate average requests per seconds using awk or sed on Apache logs

I am trying to calculate the average requests per seconds for a specific period of time of my Apache logs. I have been able to narrow the results down to the period I am interested in, as well as ...
0
votes
1answer
130 views

Having problem using sed | Trying to replace a line

Contents of gaurav.txt file $generalNetPath = "/net"; $generalNetPath = "/glbusers4/glb/aimsys/apxwrk48"; I trying to replace /glbusers4/glb/aimsys/apxwrk48 by pwd as follows qaz=`pwd` sed ...
8
votes
4answers
2k views

How do I write a sed one-liner to add a character after every third character?

So, I have a string that looks like this: AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA And I want to split the string into 3-character chunks delimited by a '+' sign. ...
1
vote
1answer
106 views

using sed for replacing strings with “/”

need to replace < network> with a "192.168.61.0/24" but i am not able to do it with sed, tried sed -i s/"< network>"/"192.168.61.0/24"/g file.sh but this is not working,can some one ...
1
vote
1answer
74 views

sed into csv format

I have a file of the form VL-8299673,30.000,49.000,1.000,21.901,2630.000,428861.000 VL-8299673,1071.000,570.000,35.000,3963.608,632.000,366563.000 ...
1
vote
2answers
108 views

How can fill characters in sed only

I have this file. I want to use only sed, no awk FSDFDSFSD FDSFD FHGDHFDHGHFGHGHGF HHGDF GFDGDFGFDG GFDGFDGFDGDFGDGD I want to fill every odd line appended with 000 so that total characters are 30 ...
4
votes
2answers
303 views

Why does this add spaces? echo “x ax” | sed 's/x\s*/x /'

I want to find a x, and replace the 0 or more following spaces (\s*) with just a single space. echo "x ax" | sed 's/x\s*/x /' For some reason, instead of replacing the spaces with the single space, ...
2
votes
3answers
504 views

Use find + sed + cp to find files and copy them to a directory with a different name

I'm trying to copy a bunch of files named folder.jpg into a folder. The problem is because all the files are named the same thing, I need to rename them in the process. I know I can probably do it ...
5
votes
3answers
1k views

How to remove all white spaces just between brackets [] using bash? [duplicate]

Replace text between brackets Input testing on Linux [Remove white space] testing on Linux Output testing on Linux [Removewhitespace] testing on Linux So, how can we just remove all the ...
8
votes
4answers
4k views

Efficient in-place header removing for large files using sed?

The commands below may takes minutes depends on the file size. Is there any more effient method? sed -i 1d large_file
4
votes
2answers
514 views

Out of memory while using sed with multiline expressions on giant file

I am currently trying to remove all newlines that are not preceded by a closing parenthesis, so I came up with this expression: sed -r -i -e ":a;N;$!ba;s/([^\)])\n/\1/g;d" reallyBigFile.log It does ...
3
votes
2answers
405 views

sort a file based on length of the column/row

I need to sort a file based on the number of chars in the first column. I have no idea on how to go about this. (On Linux, so sed/awk/sort is available). An example: .abs is bla bla 12 .abc is bla ...
2
votes
2answers
437 views

sed: replacing newline at a given line (or last line)

I have a file like the following: a,b,c,d,e,f 1,2,3,4,5,6 7,8,9,0,1,2 I need to join the last two lines with a comma, like so: a,b,c,d,e,f 1,2,3,4,5,6,7,8,9,0,1,2 The problem is that this is all ...
3
votes
2answers
119 views

How to convert *text* to {\i text} with sed?

I would like to replace every occurrence of *text* into {\i text}. text *text* text *text* text *text* *text text text* text should become text {\i text} text {\i text} text {\i text} {\i text ...
1
vote
2answers
2k views

Help inserting a new line of text after matching a line of text (sed)?

I need to insert a hidden HTML input tag into any form tag within a bunch of HTML files. I assume this is possible with sed, but need help forming the command. My idea is to search for any instance ...
3
votes
5answers
1k views

Remove lines based on pattern by keeping first n lines

I need to remove lines from a text file based on pattern but I need to keep the first n lines of that pattern. Input % 1 % 2 % 3 % 4 % 5 text1 text2 text3 output %1 %2 text1 text2 text3 I used ...
1
vote
3answers
81 views

sed replace - need help replacing text in bunch of files

How to parse bunch of files and replace each occurance of __esct function? __esct('Full name') convert to: $this->escape($this->translate('Full name')) I'm confused as to - how ...
1
vote
1answer
221 views

sed edit text block: not greedy match

I wish I can grab the first html comment in a file using sed. Using '/<!--/,/-->/ and having more than one comment block it will match all the content between the first and the last comment ...
4
votes
4answers
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 ...
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: ...
0
votes
2answers
2k views

Replace matches with multiline string using sed [duplicate]

Possible Duplicate: Insert a multiline string into another string I have a file called foo.txt containing: foo bar baz I want to replace every occurence of bar with a multiline string, ...
1
vote
3answers
591 views

linux/solaris + verify duplicate valid IP address from file

what the best way to find duplicate IP from file ( I have ksh script in this script I need to write function that check for duplicate IP ) for example if IP - 192.1926.23.52 exists twice in file - ...
4
votes
2answers
2k views

sed: multi-line replace of config block

I have some configuration files that basically look like (...content...) # BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY (... more content ...) # END DYNAMIC BLOCK (... even more content ...) Now, in ...
0
votes
1answer
374 views

wget images with certain path

im looking to only grab images from a document with a certain path for example domain.com/uploads/287167/file_name.jpg only grab those with the uploads/number/filename im currently doing it this ...
3
votes
5answers
475 views

Print non-existent files from a pipe input

I have a directory of lots of .PDF and .JPG files. There should be a .JPG file for each .PDF with the same name. I'm trying to use a command to find .PDF files that doesn't have a .JPG file. My ...
5
votes
3answers
1k views

Sed to reverse two words

echo "AXIS2C_HOME=/usr/local/Axis2C" | sed 's/\(^AXIS2C_HOME=\) \(.*\)/ \2 \1/' The output I am expecting is /usr/local/Axis2C AXIS2C_HOME=. I can't figure out what I am doing wrong. :(
1
vote
1answer
192 views

Sed and Awk learning material [closed]

I'm looking for some learning material for sed and awk, can anyone make any suggestions? Please don’t refer to man and help pages. -O-
1
vote
1answer
127 views

how to add a description in footer

I enjoy learning how to modify files with awk, sed, and bash. However, I know it could cause issues down the road and I would like to be prepared with a log inside the file. I was thinking about how ...
2
votes
5answers
533 views

Replacing lines in files with file contents

I have several files which contain some PHP includes and I want to substitute them with the file contents. The file looks like foo <?php include("file1.php"); ?> bar baz <?php ...
0
votes
1answer
313 views

globbing, sed, or awk html files

I wanted to create a script that I could check .xhtml files for images and add alt tags if needed. While searching I found regular expressions and did man on glob but I'm unsure on where or what to ...
0
votes
3answers
1k views

Split and print

I have tab-demlimted text file with 50,000lines. I want to split the first column and print as shown in output.txt Input.txt rt|371443144|mb|MN556661.1| 2200443 A rt|371443344|mb|MN556645.1| ...
2
votes
2answers
1k views

extract last match from logfile till end

Using sed, grep, awk, or ruby, what's a simple way to find the last match of a string in a larger log file, and send every line following it (inclusive) to another file? Example: grep -A222 ...
1
vote
1answer
634 views

Linux + replace STRING/WORD in file according to rule

My target is to replace any word/string/Any characters in file with new word/string according to the following rules: If numeric characters on the left side of word/string then we not replace the ...
2
votes
4answers
1k views

How to rename files with sed and csv

I'm new with Linux, sed, and awk but I don't mind challenging myself on new ideas. That being said, I understand the purpose and how to use rename and sed for a common event such as adding a $date or ...
3
votes
3answers
533 views

Script matching literal pattern over multiple lines?

I have a multi-line string in the variable $PAT. $PAT must be searched for within the file $FILE. If $PAT is in $FILE, it needs to print the file with $PAT removed. If $PAT is not found, then print ...