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

learn more… | top users | synonyms

49
votes
3answers
88k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
2
votes
4answers
93 views

Change text in tag, but only tag is contained within a certain XML block

Using Git Bash, I'm trying to conditionally replace what's in the yrot tag in hundreds of files, but only if it belongs with a part name tag that pertains to wheel. // YES, change <part ...
2
votes
5answers
120 views

Adjust gap between 2 columns to make them look straight

file1.txt: hi wonderful amazing sorry superman superhumanwith loss file2.txt : 1 2 3 4 5 6 7 When i try to combine using paste -d" " file1.txt file2.txt > actualout.txt actualout.txt : hi 1 ...
3
votes
2answers
43k views

Using 'sed' to find and replace [duplicate]

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an ...
0
votes
2answers
46 views

How to find and replace only a particular word (which is at the end of the file) using shell command?

File1: Dms 01.01.2015 feeder1 6kv close at 04:30 Updated Dms 01.01.2015 feeder1 6kv open at 06:20 Updated Dms 04.02.2015 feeder10 6kv close at 17:23 Updated Dms 04.02.2015 feeder12 6kv ...
-2
votes
1answer
54 views

Remove everything else after this mark?

Data C. Induction anesthesia, muscle relaxant, anxiolytics, 1st line sleeping disorder (but short time in market). \part{Theoretical Questions} Lorem Code cat ...
1
vote
3answers
45 views

How to grep between words in a log?

I have a txt file with lines like below. Starting With: Parameters: {"raw_message"=>"MSH....... Ending With: </HL7Message>"} I'm wanting to grep and output to a file the words between ...
1
vote
5answers
100 views

How to remove a string of characters after and before a specific character?

I have a file with thousands of lines that start with: >Miriam132_38138 Otu32555|1 I need to remove 'Miriam*********' so that each lines begins with: >Otu32555|1 The first string of ...
5
votes
3answers
3k views

sed: multi-line replace of config block

I have some configuration files that basically look like (...content...) # BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY (... more content ...) # END DYNAMIC BLOCK (... even more content ...) Now, in ...
5
votes
8answers
680 views

How do I delete the first n lines and last line of a file using shell commands?

I have a file named Element_query containing the result of a query : SQL> select count (*) from element; [Output of the query which I want to keep in my file] SQL> spool off; I want to ...
1
vote
1answer
57 views

How to write data one by one from tmp files to the final output file using for loop?

I have two tmp files named tmp1 and tmp2 which contains some lines. tmp1 file, 1c\ datafile no. 23 2c\ datafile is ok tmp2 file, 3c\ datafile no. 24 4c\ datafile is ok I have a ...
0
votes
1answer
37 views

How to remove certain number of lines every time particular string is found in file

I want to remove 1 previous line and 10 following lines every time a string was found in the file. foo.txt: } Name: john Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Line 11 Line 12 ...
4
votes
2answers
102 views

Replace 1st with 1), 2nd with 2), … in GNU Sed

Pseudocode which is a continuation from this answer gsed 's/|/1)/g' 's/|/2)/2g' 's/|/3)/3g' 's/|/5)/4g' 's/|/5)/5g' <input.csv >output.csv which is of course not working. I am interested in ...
1
vote
2answers
48 views

How to print few lines from middle of a file using unix shell commands? [closed]

My file containing the following lines, $BQ { VOL @home } database daba relation tcdeatid opendb clear .lruno := 72 <-- (This line lruno. := 72 has 10 times ...
2
votes
3answers
37 views

Truncating the precision of a date

I'm working with a bash script which polls build progress from several different systems. If a build is complete, it stats a log file to find out the last time it was edited, then feeds that time into ...
1
vote
3answers
51 views

Search word preceded by Name in Sed

This thread is motivated by the arithmetic command found in the link of this thread here in Sed. So I would like to do backward arithmetic in Sed. Data Mikael symptom David symptom hello symptom ...
1
vote
2answers
27 views

Addition of values

foo.txt: Lillypaul_sg 204803 204803 204803 Ammy_sg 3 ramaswamy_sg 3 33 tommy_sg 3 3 137374 100 Desired Out.txt : Lillypaul_sg 614409 Ammy_sg 3 ramaswamy_sg 36 tommy_sg 137480 The output is a sum ...
1
vote
3answers
72 views

Sed Replace at a specific group of positions

I have a sample input file as below: Apple Orange Gold Silver Spoon Apple Orange Gold Apple Apple Orange Apple Orange Mango Gold ...
0
votes
1answer
42 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 ...
13
votes
10answers
16k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
3
votes
2answers
87 views

shell script to extract between characters

I would like to extract a user name from the file between the keywords "/" and "NET" like below. How can I do so with programs like awk, sed or cut. from: audit: command=true ...
3
votes
4answers
226 views

Extract paragraph separated with *** using AWK

I have a file like below: blablabla blablabla *** thingsIwantToRead1 thingsIwantToRead2 thingsIwantToRead3 blablabla blablabla I want to extract the paragraph with thingsIwantToRead. When I had to ...
22
votes
6answers
52k views

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
3
votes
2answers
898 views

Split log file by time range

I have a log file and would like to cut it into copies of 15min. In the log file the lines start with: dd mmm yyyy hh:mm:ss,xxx e.g. 12 Feb 2013 16:05:02,xxx log text... and the file rotates ...
-2
votes
4answers
62 views

How to print last few contents in a line?

I have a file that has lines shown below, ta=/*92/*39/*1/*649/*12 ta=/*92/*12/*4/*342/*90 ta=/*92/*33/*9/*455/*3 ta=/*92/*12/*3/*649/*25 ta=/*92/*99/*7/*649/*7 and so on. I want a file that will ...
3
votes
4answers
87 views

To remove newlines after empty line

Data 4. Alendronic acid A. Antiosteoporotic agent. B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. C. Osteoporosis in combination with vitamin D. ...
3
votes
7answers
147 views

Insert a word in the first line of the file

Input: firstline secondline thirdline ...some magic happens here... :) Output: insertedtextfirstline secondline thirdline Question: How can I insert the insertedtext to the start of the first ...
2
votes
2answers
58 views

Inserting into middle of a string using sed

I'm trying to insert a string into multiple lines using sed, but i don't get the desired result I have this: <p begin="540960420t" end="600189590t" xml:id="subtitle8">-Some ...
4
votes
5answers
110 views

Multiline Text Processing : Editing sshd_config

I have multiple blocks at the end of my sshd_config files like theses: Match User FOO ChrootDirectory /srv/www/FOO AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp ...
4
votes
3answers
113 views

Convert /proc/cpuinfo to a JSON in linux bash

I'd like to convert the entire entry of a cat /proc/cpuinfo to a JSON of "key" : "value". Is this possible? I tried to explode the string using \n in hopes that I could get each line and then I could ...
1
vote
1answer
41 views

Insert variable in specific place using sed

Please help me i'm trying to insert variable in a file with sed but has not yet successfully(( I have variable $time and i want insert it in index.html file in specific string(line number 53): the ...
2
votes
4answers
67 views

remove the content from the beginning to the first blank line

I'm trying to remove the content from the beginning of the text to the first blank line. --($:~)-- cat example.txt pedro pablo juan francisco emiliano martin So, I would delete: pedro pablo ...
0
votes
3answers
53 views

Extract value from one file and insert it into another file replacing some other value

I have two files, whose line numbers are not same. But in file1 I want to replace lines taking reference from file2 using sed command. In file1: tid.infno := 72 tid.setnr := 120 (This number 120 is ...
3
votes
6answers
177 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 ...
2
votes
4answers
147 views

Using Grep -o or Sed/Awk to Grab Snippet from Middle of String

I am watching log traffic and see regular abuse of the various image search engines, Bing particularly. Example URL: ...
0
votes
2answers
3k views

Fastest & Most Core way to replace “\r\n” with “\n” in a file?

I suppose tr is a more core method and thus probably a faster way to replace things within a given file. However tr can only replace equal amounts of characters. meaning... 2 characters can only be ...
2
votes
3answers
54 views

Delete lines between 2 strings in Solaris

I want to delete all the line between two patterns using basic awk or sed. foo.txt : ---------------------------------------------------------------------- Cap in MB line 2 line 3 line 4 line 5 ...
4
votes
1answer
2k views

Substitute pattern within a file with the content of other file

I have a text file (devel.xml). I added the word REPLACETHIS to it in order to replace this string with the content within a different file (temp.txt). The closest thing I have is this: sed -i -e ...
19
votes
6answers
36k views

SED: insert something to the last line?

The command inserts a tag to the beginning of a file: sed -i "1s/^/<?php /" file How can I insert something to the end of each file with SED?
5
votes
6answers
360 views

sed command to swap characters

My input file layout is: mm/dd/yyyy,hh:mm,other fields I need to format it as: yyyy-mm-dd hh:mm:00,other fields sample input: 01/02/1998,09:30,0.4571,0.4613,0.4529,0.4592,6042175 ...
10
votes
3answers
6k views

What is the purpose of -e in sed command?

I can't find any documentation about the sed -e switch, for simple replace, do I need it? e.g. sed 's/foo/bar/' VS sed -e 's/foo/bar/'
1
vote
0answers
43 views

-e option in sed [duplicate]

I saw someone do this to replace "upon" with "eat" in a text file called oro. sed -i -e "s|upon|eat|" oro Question: what is the purpose of -e option in the above command? I tried with and without ...
15
votes
5answers
54k views

How to strip multipe spaces to one using sed?

sed on AIX is not doing what I think it should. I'm trying to replace multiple spaces with a single space in the output of IOSTAT: # iostat System configuration: lcpu=4 drives=8 paths=2 vdisks=0 ...
1
vote
3answers
91 views

Print previous line after a pattern match using sed?

I want to print previous line,every time a match was found. I know about grep -A and -B options. But my Solaris 5.10 machine doesn't support those options. I want solution using only sed. Foo.txt: ...
2
votes
1answer
47 views

How to output a comparison of values in separate files

With the following two files available with build names and version numbers available: server.txt Build2 200 Build3 100 Build1 400 Build7 200 local.txt Build1 300 Build2 200 Build3 100 In my ...
0
votes
3answers
143 views

How to detect end of line with sed

I'm looking for a way to only execute replacement when the last character is a newline, using sed. For instance: lettersAtEndOfLine is replaced, but this is not: lettersWithCharacterAfter& ...
4
votes
3answers
229 views

Why are these curly braces necessary in sed?

I wanted to do a range limited search in sed so I tried sed -n '195,210 /add/ p' <file> Which gave sed: -e expression #1, char 9: unknown command: `/' Purely as a guess, I tried adding ...
3
votes
7answers
145 views

Printing specific section of a line when a trigger value is present

I'm parsing data in the following format: prop1=value1:prop2=value2:prop3=value3+prop1=value4:prop2=value5 parts of the string are delimited by + properties can appear in any order the desired ...
3
votes
3answers
115 views

To replace % marks but not \% in Perl Regex

I am thinking how you can replace [^\]% marks but not \% marks in the sed -command of this answer. I think look-behind is not necessary. My current Sed command but I think Perl is a must here cat ...
2
votes
2answers
109 views

Remove first n lines from file with variable

The command below works for deleting the first 3 lines: sed -i -e 1,3d t.txt So I tried substituting the 3 with a variable in a script as below NrLines=$(wc -l t.txt) sed -i -e 1,"$NrLines{d}" ...