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

learn more… | top users | synonyms

267
votes
6answers
515k views

How can I replace a string in a file(s)?

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub ...
-1
votes
1answer
36 views

Using sed to merge two lines [duplicate]

I'm trying to merge these two lines that appear periodically in a text file. travel_ by_night_and_day I have tried: sed '/_\nby/_by/s' input.txt > output.txt but, it doesn't do anything. Do ...
0
votes
1answer
25 views

sed - match on IP octet and remove line

I am in need of a solution, preferably sed, that can match on an IP octet that contains any number combination and remove the line. For example: My file: 192.168.100.12 192.168.200.12 10.10.20....
1
vote
3answers
103 views

add text on line before match

Trying to add text on the line before that it find the match (updateKey.sh) in this case, but won't get it to work. Here is my crontab file that the script adds the line to 0 06,18 * * * /home/server/...
0
votes
1answer
25 views

Linux red hat + replace PATH or word on scripts

Under folder: /var/userTN/applo folder I have around ~100 shell scripts that include the following PATH: GLOBAL_PATH=/export/pop/PLW5 I want to replace the PATH on all the scripts under folder: ...
0
votes
1answer
39 views

Bash add text on line script [closed]

Trying to get my script to work but it won't work. Here is my script #!/bin/bash a="* * * * * /home/server/scripts/check.sh" b="0 06,18 * * * /home/server/scripts/checks2.sh" sudo sed -i '28s/^/'$b'\...
1
vote
1answer
30 views

Parsing Data File's Lines by Delimiter Instead of EOL

I'm trying to parse a large text file (600MB) of raw data which I have no control on its format. The problem is that the end of line is not placed well: head -1 filename | wc -w Produces a count ...
0
votes
0answers
23 views

Preparing update command using sed

I have a sample input file- ,4.IM-H-506X,2016-01-01 11:07:27-0500 ['LOWERCASETEST7'],JASONTEST7,2015-04-19 00:00:00-0400 "['EPROSP_IWS', '648099_EPROSP_IWS']",4.NDR-IWS-EPRO,2015-04-16 08:...
0
votes
2answers
54 views

Replace selected characters

How can one replace only the first [:space:] character in each row of a file with a \t using sed? Input: AC 12 45 DC 56 89 Expected output: AC 12 45 DC 56 89 (Something like sed "s/[:...
1
vote
2answers
39 views

Is my interpretation of `sed -i.bak '/^x /d' “$SOME_FILE”` correct?

For some reason sed has always intimidated me, its syntax is a little odd I suppose. I want to make sure I understand what the following will do. sed -i.bak '/^x /d' "$SOME_FILE" This will first ...
1
vote
4answers
127 views

How to add a new text line at the first line of a file? [duplicate]

File: TABLE1 1234 9555 87676 2344 Expected output: Description of the following table: TABLE1 1234 9555 87676 2344
6
votes
8answers
7k views

can we print the last word of each line in linux using sed command?

suppose , if there is a file consisting of following lines , if they are 12345 567 7878 66 er3 t45t y6y46y 4y6 y656y y5y 46y6 65y7 y66uyuy yy46y6y The output has to look like: 66 ...
0
votes
2answers
34 views

Add newline character to file

Hi Guys i need to change the below single line string using a shell script {1:F01NESWSZM0AXXX1111111111} {2:I940BBRUBEBBXXXXN} {3:{108:400MSOG161200FRO}} {4: :20:400MSOG161200FRO :25:1xxxxxxxx65 :28C:...
0
votes
1answer
63 views

Replace 2 successive lines using sed

I've been trying to replace successive lines with the following sed commands : sed -i -e '/string1/{N;s/string2/string2_replaced/;N;s/string3/string3_replaced}' file1 sed -i -e '/string1/{n;s/...
0
votes
0answers
36 views

awk + how to manipulate awk output

The following awk command prints the results from services file # awk '{print $2}' /etc/services 48002/tcp 48002/udp 48003/tcp How can I manipulate the output so it prints the "service file" ...
2
votes
5answers
439 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 ...
9
votes
7answers
16k views

Sed — Replace first k instances of a word in the file

I want to replace only the first k instances of a word. How can I do this? Eg. Say file foo.txt contains 100 instances occurrences of word 'linux' . I need to replace first 50 occurrences only.
3
votes
1answer
33 views

Replace a word after matched word with incrementing values

I have a file like this - ! interface csa2549abA xxxx yyyy zzzz ! interface aso23apwoeAe aaa bbb ccc ! interface FastEthernet0/1 ppp qqq rrr ! and so ...
2
votes
1answer
51 views

sed - Use file1 to find replacements contained in file2 for lines from file3 [duplicate]

I'm new here and haven't posted much, I'll try to make this as clear as I can. I want to perform a find and replace that involves three files: find.csv identifies lines containing strings. I want to ...
2
votes
3answers
47 views

linux + ssh + perform two tasks in one ssh and print to CSV

the following command print the two values in one ssh shut ssh test01 "hostname && cat /etc/sysconfig/rhn/rhnsd " test01 INTERVAL=240 but how to change the syntax in order to print the ...
1
vote
1answer
26 views

Getting rid of a line with a perfect match (not substring) using sed

I want to delete a line which matches the word name but doesn't delete the lines have something following it. I can't seem to find an answer to this. I know sed -i '/name/d' * would delete the whole ...
0
votes
3answers
51 views

sed substitution

I have a string as follows: /test/test/ 12 /test/test My goal is to turn it into: /test/est/ 12 /test/test This is the command: echo "$x" | sed 's@\(/*/\).@\1@g'" I am using at the moment but ...
0
votes
2answers
45 views

return first instance of characters from a list

Lets say I have the output of this command saved to a file. cat /dev/urandom | tr -dc '[:graph:]' | fold -w 1000 | perl -pe 's/(.)(?=.*?\1)//g' | head -n 50 I would like to compare only the first n ...
1
vote
2answers
32 views

Print indentations with whitespace only in tree command

I want to list the subdirectories of a directory using tree command. But I don't want to print indentation lines. I only want to have the whitespaces instead. I couldn't find the correct parameter ...
4
votes
1answer
392 views

How do I sort words by syllable count?

How do I perform an ascending sort of a word list, based upon how many syllables each word contains? Example Input: something important dog cat bookshelf Example Output: dog cat something ...
1
vote
3answers
67 views

Delete the nth line from a matched string (which only occurs once in the file)

I have a question related to deletion of strings that appear at the n+2 position after the matched string which is at position n using awk for multiple files. I am able to print it using the command: ...
9
votes
6answers
13k views

How to grep for text in a file and display the paragraph that has the text?

Below is the text in the file: Pseudo name=Apple Code=42B state=fault Pseudo name=Prance Code=43B state=good I need to grep for "42B" and get the output from the above text like: Pseudo name=...
2
votes
3answers
86 views

Replace values in file

I have a files: $cat file1.txt 1234|W 1345|S 8427|D 2132|C 3243|V and my sql file is : $cat select.sql SELECT * FROM CUSTOMERS WHERE ID IN (FLAG); and I have a shell with the next ...
0
votes
1answer
75 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/...
0
votes
3answers
32 views

split lines and print it to new line for certain character

I need to split lines and print it to next line when pattern match. Like I have: ABC123xxx:: 2345 ABC345yyy:: 5678 ABC986zzz:: 7955 And I want it to print to new line when ABC pattern will come: ...
2
votes
3answers
388 views

How to count the number of words and print the lines that match exactly a given pattern?

I want to count the number of words and print the matched pattern lines which matches exactly with following pattern: abc-ERROR: The input File contains: # abc-ERROR: xyxxkkfgfr # def-Error: ...
3
votes
7answers
325 views

grep with continuation lines

How can I grep/awk/sed a file looking for some pattern, and print the entire line (including continuation lines if the matched line ends with \? File foo.txt contains: something whatever thisXXX ...
1
vote
4answers
65 views

Replace the specified string to its appearance time using sed or awk

I want to replace oooo to 1, 2, 3, ..., like a example below: From > cat demo.snippet snippet anova.cca "anova.cca fun" anova.cca( ,${oooo:object} ${oooo:,...} ,permutations = ${...
2
votes
2answers
78 views

Find (and strip) common initial whitespace from file/pipe

I am looking for a way to find and remove the common initial whitespace from a text stream or file. I do not want to remove all leading whitespace (that would be a simple sed 's/^[[:space:]]*//'). ...
0
votes
1answer
69 views

Sed a plist file gives a iilegal byte sequence

With a Unix Mac 10.9.5, French CA version I've read a lot of tips but if the error is gone, the plist is corrupted. the locale command gives: LANG="fr_CA" LC_COLLATE="fr_CA" LC_CTYPE="fr_CA.UTF-8" ...
0
votes
2answers
50 views

Binding Timestamp with Hexdump?

I am running a script which is given below, sudo hexdump -v -e '1/1 "%02x\n"' /dev/ttyUSB0 | Many Pipes | gawk '{ print strftime(":: %s"), $0 }' Here the data is being hex dumped with timestamp ...
1
vote
2answers
247 views

Invalid command code with sed

This is the command I am using: system_profiler | sed -n -e '/SATA/SATA Express:/,/Software:/ p' It gives me this error sed: 1: "/SATA/SATA Express:/,/S ...": invalid command code S I don't see ...
0
votes
1answer
84 views

seeking hints for correct escaping of sed commands WITHIN yaml

I am having a hard time finding the issue why following lines of "code" break my cloud-init config. Basically I am calling a son object as part of the DigitalOcean API which has a YAML format - I got ...
0
votes
2answers
34 views

Error in yaml - possibly due to wrong escape, but unable to identify the issue

The line below causes an error (...request body malformed."}). It is part of a user-data.yml for use with cloud-init as part of the digital ocean api to bootstrap a server upon creation. sed -ie '\$a\...
0
votes
1answer
46 views

Getting “sed: -e expression #1” when trying to run search and replace

I'm using Amazon Linux. I'm trying to run a search and replace using sed. I'm running into the below error ... [jboss@mydevbox nodejs]$ sed -i -e "s/test.smokeTest('(.*?)', '(.*?)', '(.*?)')/test....
3
votes
4answers
76 views

Find matches on adjacent lines

I want to find adjacent matching lines, e.g., if the pattern matches are $ grep -n pattern file1 file2 file3 file1:10: ... file2:100: ... file2:1000: ... file2:1001: ... file3:1: ... file3:123: ... ...
3
votes
1answer
80 views

Use awk as a stream editor

awk is the swiss army knife of text manipulation. If, however, I need to change small bits in a text, I reach out to sed generally. While it might be the best tool for the job, it is worth it to ...
0
votes
2answers
38 views

Sed replace not functioning as expected

I want to replace listen = 127.0.0.1:9000 by listen = '/var/run/php56-fpm.sock' sudo sed -i 's/listen = 127.0.0.1:9000/listen = \'/var/run/php56-fpm.sock\'/g' /etc/php-fpm.d/www.conf Error: bash: -...
-1
votes
1answer
76 views

How can I print a specific occurrence of a range pattern?

I am trying to extract several data fields out of somewhat peculiarly formatted strings. Each string has a format similar to the following (spaces added for emphasis): 1 abcd 2 1 efg 2 hij 3 ...
0
votes
4answers
78 views

grep all strings that start with a certain char, and finish with another char

I need to grep all strings that start with "[" and finish with a certain string, e.g. "apal". So all chars in between these 2 chars would be shown as well. Given an input such as: [44060]apal223reaea[...
-3
votes
1answer
49 views

Removing entire line with sed not working? [closed]

I have a line which looks like this: </File2>. I want to remove it using sed, but nothing I tried worked, I tried sed '/^<\/File2>$/d' so I thought maybe theres special characters at ...
1
vote
2answers
20 views

Deleting lines by matching 3rd and 4th character only

I am running a script that fetches me the data as given below, 1400d2b8 1f4d1e2507 1200781e As you can see the 3rd and 4th character is 0 in line 1 and line 3. I am trying to write a single line ...
1
vote
1answer
33 views

How to output multiple lines using a single condition?

I have a file with the pattern: n0 n1 n2 ... ni ------------------------------- N0 N1 <empty line> if N0 is less than a specific number, I want: 2 lines above the N1's line N1's line Empty ...
3
votes
1answer
142 views

Replace with sed until match in a line

I have to replace all "." characters before the "=" character in every line in a file. The lines are like this: one.two.three=something four.five=1 six.seven=127.0.0.1 eight.nine.ten.eleven=...
0
votes
1answer
34 views

Printing all lines around a string until matching another string [duplicate]

I have an example dataset where a section has fields beginning with "something". I would like to obtain all of the lines in each "something" section when matching a specific string of "1234". I was ...