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

learn more… | top users | synonyms

2
votes
3answers
68 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
15 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 ...
9
votes
7answers
56k views

How can I use variables when doing a sed?

I want to do: cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh in my program. But I want to use variables, e.g. old_run='old_name_952' new_run='old_name_953' I have ...
3
votes
3answers
55 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
votes
5answers
62 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" /></...
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 ...
5
votes
5answers
119 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): ...
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
173 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
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 ...
4
votes
3answers
395 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
2answers
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
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,...
0
votes
2answers
40 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
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 ...
7
votes
3answers
598 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
315 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
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"...
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
447 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
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
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
413 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
51 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 ...
7
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
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 ...
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
11k 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
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
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
44 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
561 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"...