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

learn more… | top users | synonyms

1
vote
2answers
153 views

Replace only first instance of a character

for example I have scv file which looks like a1, b1, c1, d1 a2, b2, c2, d2 a3, b3, c3, d3 What I want to do is to replace the first comma , with the semicolon ;. The position of first comma can be ...
1
vote
3answers
48 views

Appending a character in the nth position of a matching string

I need to find a sed/awk command which will insert a character at the nth position of a matching string HAVING LENGTH 10. For example, I need to search for the string starting with pattern 541 and ...
4
votes
3answers
374 views

Recursively find all files named “file.txt” and execute a sed command

The challenge of my task is that the file.txt can be in subfolder or in sub-sub folders. The general structure looks like this: folder |___subfolder |_____file.txt | |...
4
votes
3answers
3k views

grep one line before the match plus the match

zzzzzzzzz aaaaaaaaa bbbbbbbbb & ccccccccc & ddddddddd hhhhhhhhh eeeeeeeee fffffffff & ggggggggg & in the above line, what i want is to grep/sed/awk (any method is fine) line that ...
2
votes
4answers
38 views

Merge lines between keywords into one-line comma separated values

Between first occurrence of Cat to next occurrence of Cat, it should create a separate line with delimiter as ",". File input as below. Cat AA BB CC Cat AA-1 BB-1 CC-1 Output expected: Cat,AA,BB,...
4
votes
5answers
84 views

Non-greedy match with SED regex (emulate perl's .*?)

I want to use sed to replace anything in a string between the first AB and the first occurrence of AC (inclusive) with XXX. For example, I have this string (this string is for a test only): ...
0
votes
2answers
34 views

Extract the value part from key-value pair separated by colon in Linux

I need to fetch arn:aws:sns:us-west-2:596873354795:demo from the below content in a file using sed command. What would be the command? Sample file: { "TopicArn": "arn:aws:sns:us-west-2:...
2
votes
1answer
60 views

Why do my single quotes vanish on substitution with sed

What is the right command for: sudo sed -i '/, false);/adefine( 'WP_MEMORY_LIMIT', '64M' );' /var/www/html/wp-config.php The result is: define( WP_MEMORY_LIMIT, 64M ); instead of the desired: ...
2
votes
1answer
53 views

Can't do substitution in a sed program

Q. I want to write a 2 line program that lets me substitute "the" with "zee" and "The" with "Zee". Here is what I'm doing: I'm writing a sed program for example, program.sed. Inside it, I'm ...
7
votes
3answers
584 views

Replace all occurences of two spaces after the end of a sentence with just one space

I'm stuck in including regular expressions with a sed command. Q: I want to replace all occurences of two spaces after the end of a sentence with just once space. Here is what I did: sed 's/^ ...
3
votes
2answers
312 views

Don't understand the 'n' command of sed

I'm reading sed & awk book and I thought that I understood the n command of sed till I executed the following: $ echo -ne "abc\ncde\nfg\n" | sed "/c/{ n /f/d }" Output: abc cde fg But I ...
4
votes
2answers
209 views

Replace the first occurence of a pattern in a file that may contain a slash

Thanks to http://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed I know how to pass a variable that contains slashes as a pattern to sed : sed "s~$var~replace~g"...
1
vote
0answers
50 views

How to use ! in the sed command?

Apologize if you feel this question very basic. Anyways, I am typing sed '/[iI]t/ a\\ Found it!' data but it says the error event not found. I tried escaping that ! with backslash \! but it doesn'...
4
votes
1answer
446 views

Sed command that would ignore any commented match

I'm trying to create a sed command using regex in order to substitute something in a text file only if it is not commented but I'm running into some troubles due to my almost non-existent knowledge of ...
-3
votes
0answers
33 views

sed replace string in file with shell

Line in the file : def customerName = "TEST" Want to replace the TEST by the script argument input($1). Shell Script: The command which is giving TEST as output is : strReplace=`cat $filePath | ...
-3
votes
1answer
54 views

Extracting two lines if pattern exist in the second line + clean + remove duplicates (Whatsapp extraction) [duplicate]

I want to search through a WhatsApp conversation (dumped as txt file) for any message containing the string "I will tell to your mom" as well as the message before it. However, I need to be able to ...
6
votes
4answers
412 views

How can I blank the nth to mth field using the awk command?

I would like to solve the problem below using AWK. If any other solutions are possible using languages such as sed or Perl, that would also be much appreciated. Below is the input: U,N,UNIX,000,A,5 ...
0
votes
1answer
45 views

sed works on commandline but not in script [closed]

Why does this work when I type it direct on the commandline: oldversion=12345 newversion=67890 sed -i "s/${oldversion}/${newversion}/g" "/home/user/MyDir_${newversion}/MyDir_${newversion}.reg" But ...
3
votes
2answers
142 views

SED challenge,aggregating String containing braces

I've been trying to use sed for aggregating a specific string pattern within a text for hours, but can't find the solution. I hope you guys know how to do it!? The text subject to my Goal consists ...
2
votes
1answer
50 views

using sed with ampersand (&)

i'm using sed to find and replace patterns within SAS files i have rather than changing them individually. The problem is I am trying to replace macro variables and when i use the ampersand it is not ...
17
votes
4answers
32k views

How to insert text before the first line of a file?

I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1: sed '1 a\ But I want to add it before line 1. It would be: sed '0 a\ but I ...
10
votes
5answers
36k views

How to add a carriage return before every newline?

I have a file that only uses \n for new lines, but I need it to have \r\n for each new line. How can I do this? For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a script ...
2
votes
3answers
92 views

Are there any sed scripts built in to mainstream Linux distros?

When I was studying bash, I found it very helpful to go and study the bash scripts already present on a clean install of Linux—/etc/profile, for instance, and anything in /etc/rc.d/init.d/. These are ...
1
vote
2answers
48 views

how to insert file contents on match using sed - first occurrence only [duplicate]

file contents: # cat file.txt ----- MATCH ----- MATCH ----- MATCH ----- # cat text.txt add this text file before first match sed commands: # sed '0,/MATCH/r text.txt' file.txt ----- add this ...
3
votes
1answer
30 views

Awk and discarding non-matching tokens in a string?

I'm having trouble getting a string tool to provide what I need (due to my ignorance). I have a string based on CPU features. The string will change as different processors offer different features: #...
0
votes
0answers
30 views

How To Create Soft Links To An Entire Directory Structure Which Has Sub-Directories, Files Etc! [duplicate]

I'd like to: Create a soft link for a directory or file. But have a scenario where I need to create soft links, to a directory which has multiple sub-directories, files etc. I wanted to loop across ...
6
votes
1answer
5k views

remove lines where a field's value is less than or equal to 3 - sed or awk?

I need to remove every line that has a value of 2 or less in the 8th field (column). My data looks like this: 12-31 Airport 189 379 41 49.70946503 -124.91377258 2 2880 30.8 01-01 ...
2
votes
3answers
173 views

How to remove all lines that have one value in the line?

I have a file as the following input file 0.0 4 6 5 1 2 9 4 5 1 ..... 0.25 0.5 3 1 1.0 3 7 8 2 1.5 3 3 3 4 6 4 5 2.0 2.5 6 7 6 9 I want to delete all lines ...
1
vote
2answers
50 views

Replace particular character but not if it is inside ()

I'm looking for a one-line command to make a file more readable. I want to replace all ; characters with newline unless it is inside a set of (). This is on a firewall, so I can only use bash; no ...
1
vote
1answer
35 views

sed error: “\1 not defined in the RE” under OS X

my question is: how to remedy the error yielded by the last command below, under OS X 10.10.5? $ ls foo* foo123bar.pdf $ for f in foo*; do echo "$f" | sed -E 's:foo:bar:';done bar123bar.pdf $ for f ...
1
vote
3answers
64 views

How to grep/awk/sed for text in a log and display the chunk that has the text?

I'm looking for something pretty similar to this. The logs look like this: [09:44:22] [main] ERROR [url/location] - A ONE LINE ERROR [09:44:22] [main] ERROR [url/location] - Another ERROR [09:44:22]...
2
votes
1answer
48 views

how to append words of two files word by word [duplicate]

sorry for my newbie question. I have two files with the same number of words and column fields in each line. I want to append these two files word by word. For example: File 1: A1 B1 C1 D1 E1 B1 C1 ...
6
votes
5answers
10k views

Remove backslashes from a text file

I have Input: NISHA =\455 Output: NISHA = 455 I want to remove \ from output. I tried to use command sed "s/[\]//g" P but it is not working and it flags an error: character found ...
1
vote
1answer
35 views

remove lines that contain IP range from specific column while making exclusions to range

I have a logfile with the following format: Jul 13 21:47:41 192.168.100.254 "user from 192.168.100.101" I need to remove ALL lines that contain IP's in the 192.168.x.x range but only if they appear ...
1
vote
2answers
29 views

Error in format when appending column with sed

I have a table and I want to add a column containing, for each line, the name of the file itself. I will post an example below: This is the input: SPATA17 1 217947738 LYPLAL1 1 219383905 FAM47E ...
0
votes
2answers
64 views

deleting lines between rows in a text file using awk or sed

I am wondering if there is a sed or awk command to delete all lines between the 'Query_' headers in column 1, if the number of lines between each header is less than 5. The following is an extract ...
0
votes
1answer
41 views

reformat values in a table

I have some tables, in which I need to replace several values. I have different commands sed and tr that work just fine when I apply them to a single file. But somehow, when I want to build a loop ...
0
votes
1answer
43 views

How do I comment lines in fstab using sed?

I want to comment certain lines in fstab using sed command. The following are the lines I need to comment: 172.0.0.1:/export/project/common /nfs/share nfs4 rw,bg,hard,nointr,rsize=131072,wsize=131072,...
1
vote
3answers
50 views

I need the counts of lines between two matching patterns

I have my input like below - [11/Jul/2016:13:40:43 +0000] "POST --" 200 - 7 [11/Jul/2016:13:40:43 +0000] "POST --" 200 - 7 [11/Jul/2016:13:40:47 +0000] "POST --" 200 - 7 [11/Jul/2016:13:40:47 +0000] "...
7
votes
2answers
557 views

Grep to find the correct line, sed to change the contents, then putting it back into the original file?

I'm trying to change a single word on a specific line in a file, but I'm having some trouble connecting all together. Basically, on one line in my file there is a keyword 'firmware_revision', and on ...
0
votes
1answer
22 views

Replace value in a table

I have some tables for which I need to replace a field that has a random position in each table. For information, the table are semicolon separated fields and I would like to replace the field "datum"...
1
vote
1answer
48 views

is it possbile to add comma in this SQL?

I have a statement like this UPDATE `test`.`scores` SET id=1 name='kobe' score=99 WHERE id=1 name='kobe' score=100 UPDATE `test`.`scores` SET id=2 name='tmac' score=70 ...
1
vote
2answers
44 views

How to search and replace a string with newlines on commandline in a file

I want to replace all occurrences of a text string in a textfile on command line on CentOS 7.2. Search-string: ).\nPORT Replacement-string: ). \n0 closed ports\nPORT I know, this can be achieved ...
-1
votes
3answers
47 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" /></...
7
votes
7answers
346 views

grep the exact block of lines (content of file1) from file2

I have two files, file1 and file2. The sample content of file1 is: A B C D E F G H and the content of file2 is like: A B few other lines E F few more other lines A B C D E F G H few more other ...
5
votes
4answers
240 views

sed/awk remove newline on two pattern matches

I have a .pem file that after some formatting it shows like this: -----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAoK3D4mMIRnzPaYqHidgpBnDDzLlcYYd0GoB0pQGyGSHDW7KO3K+VeJP90GhE ZTEWJLp2N5DR/KT+5Vg7cgdx/...
2
votes
5answers
94 views

Replacement of XML based on attribute content using sed

I've got to replace some attribute content in an XML tag, depending on a parameter $1. We've got in input, for example: <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="OO ...
1
vote
5answers
69 views

Getting substrings in between delimiters multiple times from one string without looping

I have looked all over for a solution to this, but I couldn't find one. I am using qstat -x to pass a large string of job information. The output from qstat -x is in XML format. The substrings I am ...
4
votes
5answers
1k views

Merge alternate lines from two files

File1: .tid.setnr := 1123 .tid.setnr := 3345 .tid.setnr := 5431 .tid.setnr := 89323 File2: .tid.info := 12 .tid.info := 3 .tid.info := 44 .tid.info := 60 Output file: .tid.info := 12 .tid....
0
votes
2answers
107 views

sed use to replace string 2 lines after matching pattern in AIX 6.1

I try to replace a character, 2 lines after matching pattern. For this, I'm using this code: sed '/some_pattern/{N;N;s/word1/word2/}' /etc/filesystems > /etc/filesystems.tmp && mv -f /etc/...