"sed" ("stream editor") is a Unix utility that parses and transforms text files.

learn more… | top users | synonyms

2
votes
2answers
145 views

sed: How can I replace \r\r\n?

In this question on stackoverflow it is explained how one can replace a newline character using sed. My problem is that I want to replace the character sequence \r\r\n with sed. Can somebody help me ...
1
vote
2answers
475 views

Bash substring last occurance of a character

I am trying to write a script that removes everything after the last occurrence of a character. For example if the string is DATETIME 2014.03.14 12:23:24, I would like to get just DATETIME 2014.03.. ...
0
votes
1answer
73 views

Extract stings between a delimiter if it matches a certain string

File cat hosts.cfg define host { host_name switchlan alias switchlan address ...
0
votes
1answer
68 views

How to find the matching records from 2 files in unix

I have two files which contains email_ids. 1. Test1.txt 2. Test2.txt Test1.txt contents are: abc@gmail.com xyz@gmail.com mns@gmail.com Test2.txt Contents are: jpg@gmail.com joy@yahoo.com ...
0
votes
1answer
209 views

sed exact match patterns of range and extract lines from thereon

I am trying to extract range from IP= to TUAL_HOSTNAME_=, as below. But its also extracting other match pattern for IP. sed -i -e '/IP=/,/TUAL_HOSTNAME_=/{w /tmp/101' -e 'd}' /tmp/install; #IP= ...
0
votes
1answer
64 views

Sort file with respect to a part of a column

I have one file that contains the details of space in data stores. I want to sort it according to the Free space available in them. File is : Name:VSP-01-32 Capacity:2047 Free Space:144 ...
1
vote
1answer
136 views

How to replace line in file with pattern with sed?

I'm reading a lot of documentation on sed, and am still stumped on my particular use case. I want to replace this line in a conf file with my own line: Replace this line: #maxmemory <bytes> ...
0
votes
1answer
310 views

How to replace text strings by reading in list using sed

I'm trying to come up with a sed script to run from the shell. I need to be able to read in a list of UUIDs from an external file, which contains two columns origtexta replacementa origtextb ...
0
votes
3answers
40 views

Removing a pattern from text file

I have a file riddled with this kind of thing: -POEM- >> The big brown fox >> jumps over the >> lazy dog. The end. I want to get rid of "\n>>" and just turn this into a oneliner: ...
0
votes
1answer
71 views

Linux/Bash: Comment every line matching a pattern

sed is foreign to me, to say the least. I'd like to traverse a C project tree and comment out every line calling the function LogMsg(). Something that'd achieve the following: foreach: line within ...
0
votes
2answers
91 views

Replace string in string but not when its somewhere between double square bracket

I have a long string from mysql field containing the data of a mediawiki page. I have to replace a string in that mediawiki page but not when the string is in mediawiki link. The mediawiki link is ...
2
votes
2answers
106 views

How can sed find pattern in one field but append another(same row)

I have a text file that has comma separated values. Field1 has field that needs to be matched, but field two needs substitution based on the match. Example: A,C B,C Result: A,D B,C I'm guessing ...
0
votes
3answers
266 views

how can sed get patterns from a file

I have two files one holds a list of patterns, another need to be changed based on these patterns. So far I have been experimenting with cat patterns.txt| xargs -n1| sed ...
0
votes
1answer
50 views

How to encrypt the mail in unix?

I want to encrypt the mail as that will be containing the confidential information. There are no attachments. Please help in providing the steps to encrypt the mail. I have got the reference of How ...
0
votes
3answers
61 views

How do I exclude match and change only specific character with sed

I am trying to use sed to replace '0' with the word 'correct' (I am using echo $? to validate my script and I want to replace the output of echo). The problem is that I have more text in the output ...
0
votes
1answer
80 views

How to delete a line from a file that requires permission such as visudo using sed?

I want to add this command timestamp=20 at the end of visudo file which will increase the sudo time for 20 min. This way I'll be able to use my bash script to install many applications. After I am ...
0
votes
1answer
43 views

editing string using sed or grep

first of all please execuse my ignorance on UNIX. I am quite new. I have lot many sql files that contains following string ${DB_USERNAME}.${TABLE_NAME} Can anyone please tell me how could I change ...
0
votes
1answer
87 views

How to insert a comma after every 8th character of a line using sed

How do I insert a comma after every 8th character of a line that has alphanumeric characters including blanks?
3
votes
2answers
441 views

How to comment out multiple line matching a regex using sed or awk

I have a file that contains this: [...] location /static { ... multiple lines ... } [...] location /static/ { ... multiple lines ... } [...] And I want to get: [...] # ...
1
vote
2answers
59 views

Is there a way to print 2 lines (if any) before and after a target or print a placeholder if not?

I'm not native english speaker so I hope to be clear. I know about grep -C 2 "TARGET" inputfile to select 2 rows before and after the row of the TARGET, but I'm not able to use it to manage my ...
1
vote
1answer
27 views

Is it possible to sed -i from a text file to another text file?

I'm coding a shell script on my Debian box and there's a line on the script that needs to delete some emails from a textfile that is full of emails, like this: sed -i ...
1
vote
3answers
101 views

How to select a section from a string in using bash

I have an array containing strings which consist of paths to files in the following format: /path/to/file/14561234545_50303.TXT I'm trying to select the section that is after the "1" but before the ...
3
votes
2answers
674 views

How append a string at the end of all lines?

I'm trying to add a string at the end of all lines in a text file, but i have a mistake... :( Example: I've this in a text file: begin--fr.a2dfp.net begin--m.fr.a2dfp.net begin--mfr.a2dfp.net ...
2
votes
3answers
702 views

Bash script, string not replaced correctly with escaped $ and & characters

I have created a shell script which takes a string as input. If the string contains $ or &, then I need to replace those characters with \$ and \&. I could done that with sed command. After ...
0
votes
3answers
33 views

How to add a character like # to the begining of the 1st nonblank line for several files?

I have a bunch of files, which I would like to use # to comment the 1st non-blank line, the files are not in good format, so there may be 1 or multiple blank lines before the line I want to comment. ...
0
votes
2answers
57 views

Redirect a cat command when using wildcards as input

I have the following command that takes a certain part of a file in the current directory, writes it to a temporary file, gets a part of another file one directory up, writes it to a temporary file ...
-1
votes
2answers
65 views

how to find number of substrings between delimiter? [closed]

I have the string following something(1)^^^something(2)^^^something(3)^^^ ... ^^^something(n) how to find number of something(s) in the string.
0
votes
1answer
74 views

iterating bash script for cycle in a file

I try to make this very simple bash program that must make the following operations: modify a file java n times: every time I must change a single word in a single line of the file (the line is 11) ...
1
vote
2answers
258 views

batch find/replace using sed or awk

In every file with filename matching pattern1 I want to search for lines which start with pattern2, and add some string in a new line above those matched lines. As a simple example suppose I'm ...
0
votes
1answer
27 views

How do I insert a line at a line of a file in sed for windows?

if i have a file half a pound of tuppenny rice half a pound that's the way the money goes pop goes the weasel And I want to insert a line "of treacle" at line 4. so it should be half a pound of ...
1
vote
4answers
504 views

How to split CSV files as per number of rows specified?

I've CSV file (around 10,000 rows ; each row having 300 columns) stored on LINUX server. I want to break this CSV file into 500 CSV files of 20 records each. (Each having same CSV header as present in ...
0
votes
2answers
126 views

Find and replace with sed, keep original line too

I have param1='123' I would like param1='123' param2=123 where 123 will change between the files I'd like to run this on. I can get param2 using sed -i "s/param1=\([0-9]\+\)/param2='\1'/g" ...
2
votes
4answers
3k views

Print line X lines before/after found line

I want to search for a particular string in a certain file. If I found the string, I also want to print the line X lines before (or after) that line. Can this with grep or awk, or do I need a ...
0
votes
1answer
110 views

Find number in string and replace it

I wrote C++ code and now I want to replace the number in a string by using shell scripts. The part that needs to be replaced: if (random<=90) How I can change the 90 from a shell script? I ...
1
vote
1answer
48 views

AWK does not work correctly in my system [duplicate]

i cant run this code because of an error awk 'BEGIN {x=0;y=0} { match($13,/([0-9]+)/,a); x = x + a[1]; match($12,/([0-9]+)/,b); y = y + b[1]} END {print "rxPackets:" x " txPackets:" y}' yourfile ...
2
votes
3answers
2k views

Extract URL from text with multiple URLs using sed

I have a string with a text and several URLs. How can I extract one specific URL (of a spoecific domain) with sed? For example, I have this: Text foo bar Text foo bar <br /><br /> ...
0
votes
0answers
32 views

Seperate one data file into 2 files

I would like to seperate 1 file into 2 or 3 files. (Normally I open .txt file, check second column and first, if it has different numbers and copy that data to Input a.txt:) ...
0
votes
2answers
58 views

Including " in a regular expression using sed on windows

I have: echo finally came. 34^"This is the account of | sed "s:[1-9][0-9]["]:<a name=\"top\">&</a>:g" and I need to get finally came. <a name="top">34"</a>This is the ...
0
votes
1answer
30 views

How to print statistics from lines in a file

I often find myself computing percentages manually after filtering through a log file to find the proportion of X or Y in it. Can this easily be achieved via common CLI tools?
0
votes
3answers
33 views

Almost-flattening a directory structure

I have a bunch of directories in a structure like this: -Music -Artist1 -Album1 -Album2 -Artist2 -Album1 -Album3 -Artist3 -Album2 -Album4 All the directories will ...
1
vote
1answer
2k views

sed: can't read : No such file or directory

I use this as str_replace for every file in folder after unzipng archive in temp folder find "$tmp" -type f | xargs sed -i "s/${targetsubstring}/${newsubstring}/g" but I get this error: sed: ...
0
votes
1answer
34 views

SED basic substitutoin is not working

I am on ubuntu 12.04. Following expression: echo abcd123 | sed 's/\([a-z]*\).*/\1/' is supposed to get result as "abcd". But I am getting "abcd123". WHY?? Using following site as SED tutorial: ...
0
votes
2answers
526 views

Replace on Nth Occurrence

I have This Code sed '/Sometexts/ r newtext.txt' old.txt > new.txt This Replaces The String 'Sometexts' with the contents of text file newtext.txt but i want to replace on the 2nd ...
0
votes
2answers
153 views

Replace a Paragraph After a String

I have a text file with a lot of lines and paragraphs I want to replace a string with a line of string Example: Old Text File Sometexts New Text File Sometexts With Some Lines Paragraphs and ...
0
votes
2answers
138 views

split a textfile after each n matches to a new file using sed or awk

i tried to split a file in parts of n matches each. The file is just one line and the seperator is '<br>' foo<br>bar<br>.....<br> I just want to split the file in parts, ...
1
vote
1answer
134 views

Write text into first lines of every file

I want my web application to be free software, with a GNU license. I've read that I should add my name and the license in every file. I have a lot of files, so I guess that I could execute a command ...
-1
votes
1answer
28 views

Delete multiple lines with sed [closed]

HI I want to delete lines with this kind of text: lorem boringfunction("ads"); insum boringfunction("qwe"); lorem to lorem insum lorem
0
votes
1answer
45 views

Move numbers in a filename to the start

I have several files in a folder, with names in the form: SOMETEXT1 number SOMETEXT2.mp3, or SOMETEXT number.mp3. I want to rename these to number SOMETEXT SOMETEXT2.mp3, or number SOMETEXT.mp3. Using ...
3
votes
1answer
230 views

Replace special characters with set from Windows cmd

I need to replace the string "\\" (double quote backslash backslash double quote) with "" (double quote double quote). The following is an example of the data to be manipulated: ...
0
votes
3answers
861 views

Unix - Surround First Column of CSV with double quotes

I have data in the following format - 4,"abc" 8,"def" 9,"ghi" I want to surround the value of the first column by double quotes. "4","abc" "8","def" "9","ghi" How do I do that ?