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

learn more… | top users | synonyms

2
votes
4answers
94 views

awk / sed / etc. concatenating colums in one file

I wonder what is a simpler way to do this: awk 'NR > 1 {print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"$9$10$11$12$13$14$15$16}' file.in > file.out which is simply speaking " ...
0
votes
0answers
16 views

How to escape special characters in gnu sed? [duplicate]

I have variables in shell: VAR_IN='Unsafe \t \ string I don't control.' VAR_OUT='some output' I need to change every VAR_IN to VAR_OUT in a stream. Idea is to: sed "s/$VAR_IN/$VAR_OUT/g" # this ...
3
votes
2answers
27 views

Simple significant usage of '/M' multi-line address suffix?

For GNU sed v4.2.2-7, info sed says: '/REGEXP/M' '\%REGEXP%M' The 'M' modifier to regular-expression matching is a GNU 'sed' extension which directs GNU 'sed' to match the regular ...
3
votes
3answers
56 views

`sed` regexp error

I'm trying to write an alias to get the ip of a docker container. The command is the following: docker inspect redis | grep IPAddress | awk 'NR==3{ print $2 }' | sed 's/[^"]*"\([^"]*\)".*/\1/' If I ...
1
vote
2answers
47 views

Use sed to pick out a potentially multi-line log entry

I've been trying to come up with a sed command to pick out a single log entry from our app server logs. The logs are in this format: LOG|2016-07-26_15:37:45:536|entry1|! LOG|2016-07-26_15:37:45:536|...
0
votes
1answer
32 views

Using sed to uppercase the first non-numeric character, leave others as is

I'm trying to convert character strings like "123456a" to "123456A" or "test" to "Test", but leave existing uppercase as is, for example testHW becomes TestHW. I have tried many attempts circling ...
0
votes
0answers
32 views

Output text between two strings [duplicate]

I have nmap output that is in a format similar to the following: Nmap scan report for 192.168.2.1 Host is up, received user-set (0.0055s latency). 23/tcp closed telnet reset ttl 55 Nmap ...
1
vote
2answers
175 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 ...
4
votes
3answers
396 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 | |...
2
votes
4answers
39 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,...
5
votes
5answers
121 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): ...
2
votes
1answer
61 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
54 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 ...
3
votes
2answers
317 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 ...
0
votes
2answers
41 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:...
7
votes
3answers
599 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/^ ...
1
vote
3answers
53 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 ...
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
2answers
213 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"...
-3
votes
0answers
35 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
56 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 ...
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
52 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 ...
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
31 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 ...
1
vote
1answer
36 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 ...
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
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]...
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 ...
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 ...
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
65 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
45 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,...
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
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
2answers
46 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
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
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
562 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 ...
5
votes
4answers
245 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
1answer
127 views

Sum of columns for rows containing a specific term

I'm wondering if there is any sed or awk command that can sum all columns for the rows with an identical identifier. For example, my file data.txt is set out as below, except that it contains ~1800 ...
7
votes
7answers
354 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 ...
4
votes
3answers
106 views

How to print same lines multiple times with variables changed using Sed/Awk/ anything?

a. Have set of lines as given below. cr_v8_sel0 : cross cp_v8_en, cp_sel0 { ignore_bins ig_v8_sel0 = binsof(cp_v8_en) && binsof(cp_sel0) intersect {[0:3],[5:$]}; } b. I wanted to print ...
3
votes
1answer
39 views

How to print a pattern of text in color?

RED='\033[0;31m' NC='\033[0m' # No Color var='I /love Stack Over/flow' I am trying to print a portion of text using regex in color but it's not working. echo $var | sed "s=\(.*/\)\(.*\)=\${RED}\2\$...
0
votes
2answers
35 views

Remove string that has special characters

I have a file called test similar to the following: hello my name <h6>test morning</h6> is bob I know I would use: sed -i -- 's/name//g' test To remove name from the file, but how do ...
0
votes
0answers
18 views

How to add a # to the begining of a line, only specific lines that I grep for [duplicate]

I have a file that has many thousands of lines. I what to comment out only specific lines in the file. I want to add a "#" to the beginning of the greped for line plus 2 lines above and one line below ...
5
votes
3answers
138 views

What is the difference between the sed commands `n` and `b`?

Other than the obvious syntactical point that b can accept a label while n does not, and that to use b without a label you need to place it at the end of an argument passed with -e (for one-liners, at ...