I understand sed will    Applies all instructions on line 1, then line 2... Previous instructions may change the pattern space so the next instruction will operate on that modified pattern space (instead of the original text)   The example is:  sample.txt file  pig cow  Command  sed -e 's/pig/cow/; s/cow/horse/;' sample.txt  Expected Output  horse horse  Real Output  horse cow  The reason for my expected output is:   First instruction substituted pig with cow in the pattern space Second instruction substituted 2 pigs with 2 horses in the pattern space
123368
I know people use : instead of / when they are dealing with paths, but  what does :g do in the following sed action?  Why is path1 between quotation mark? Can anyone explain the action of this code?  sed -i -e s:INPUT_REPLACE:"${path1}":g ${path2}
25907
Original file claudio antonio claudio michele  I  want to change only the first occurrence of &quot;claudio&quot; with &quot;claudia&quot; so that I would get the following: claudia antonio claudio michele  I have tried the following: sed -e '1,/claudio/s/claudio/claudia/' nomi  The above command performs global substitution (it replaces all occurrences of 'claudio') . Why?
50223
I am running the following 2 sed commands. The first one adds newline characters where I want them, the second also adds newline characters where I want them, BUT also adds an extra one at the end of the file where there wasn't one before.  sed -e 's|\&lt;LIST_G_STATEMENT&gt;|&amp;\ |g' ${XMLDIR}/statement_tmp_1.xml &gt; ${XMLDIR}/statement_tmp_2.xml  sed -e 's|\&lt;/LIST_G_STATEMENT&gt;|&amp;\ |g' ${XMLDIR}/statement_tmp_2.xml &gt; ${XMLDIR}/statement_tmp_3.xml   Using od -c on all 3 of the files gives the following output.  statement_tmp_1.xml (no \n at end of file)  1314700    T   A   T   E   M   E   N   T   &gt;   &lt;   /   L   I   S   T   _ 1314720    G   _   S   T   A   T   E   M   E   N   T   &gt;   &lt;   /   G   _ 1314740    S   E   T   U   P   &gt;   &lt;   /   L   I   S   T   _   G   _   S 1314760    E   T   U   P   &gt;   &lt;   /   A   R   X   S   G   P   O   &gt; 1314777   statement_tmp_2.xml (no \n at end of file)  1314700    S   T   A   T   E   M   E   N   T   &gt;   &lt;   /   L   I   S   T 1314720    _   G   _   S   T   A   T   E   M   E   N   T   &gt;   &lt;   /   G 1314740    _   S   E   T   U   P   &gt;   &lt;   /   L   I   S   T   _   G   _ 1314760    S   E   T   U   P   &gt;   &lt;   /   A   R   X   S   G   P   O   &gt; 1315000   statement_tmp_3.xml (\n at end of file - where did it come from?)  1314700    S   T   A   T   E   M   E   N   T   &gt;   &lt;   /   L   I   S   T 1314720    _   G   _   S   T   A   T   E   M   E   N   T   &gt;  \n   &lt;   / 1314740    G   _   S   E   T   U   P   &gt;   &lt;   /   L   I   S   T   _   G 1314760    _   S   E   T   U   P   &gt;   &lt;   /   A   R   X   S   G   P   O 1315000    &gt;  \n 1315002   I am running AIX 5.3  Basically, I either want it to stop adding the extra \n, or find a way of removing it.
11166
I would like to update a large number of C++ source files with an extra include directive before any existing #includes. For this sort of task, I normally use a small bash script with sed to re-write the file.   How do I get sed to replace just the first occurrence of a string in a file rather than replacing every occurrence?  If I use  sed s/#include/#include "newfile.h"\n#include/   it replaces all #includes.   Alternative suggestions to achieve the same thing are also welcome.
104899
I have a file that contains filenames with absolute path.  In the middle of that path, is a variable directory, whenever I hard code the variable part, it works fine, but when I replace it with the variable name, the sed part doesn't work.  The sed part is to delete the pattern or for this matter, the path.   file=`echo $one_file | sed 's:/dir1/dir2/$VAR/last_dir/::'`
8341
I'm going to submit form using cURL, where some of the contents is come from other file, selected using sed  If param1 is line matching pattern from other file using sed, below command will works fine:  curl -d param1="$(sed -n '/matchpattern/p' file.txt)" -d param2=value2 http://example.com/submit   Now, go to the problem. I want show only text between 2 matching pattern excluding the matching pattern itself.  Lets say file.txt contains:  Bla bla bla firstmatch It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. secondmatch The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.   Currently, lots of "beetween 2 matching pattern" sed command won't remove firstmatch and secondmatch.  I want the result to become:  It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
92184
This  inserts a tag to the beginning of a file:  sed -i "1s/^/&lt;?php /" file   How can I insert something to the end of each file with sed?
14743
To all the 'sed' doctors out there:  How can you get 'sed' to extract a regular expression it has matched in a  line?   In other words words, I want just the string corresponding to the regular  expression with all the non-matching characters from the containing line stripped away.  I tried using the back-reference feature like below  regular expression to be isolated           gets `inserted`                here                     |                v    sed -n 's/.*\( \).*/\1/p    this works for some expressions like   sed -n 's/.*\(CONFIG_[a-zA-Z0-9_]*\).*/\1/p    which neatly extracts all macro names starting with 'CONFIG_ ....' ( found in some '*.h' file ) and prints them all out line by line             CONFIG_AT91_GPIO           CONFIG_DRIVER_AT91EMAC                    .                    .              CONFIG_USB_ATMEL           CONFIG_USB_OHCI_NEW                    .                  e.t.c.    BUT the above breaks down for something like    sed -n 's/.*\([0-9][0-9]*\).*/\1/p    this always returns single digits like                    7                  9                  .                  .                    6   rather than extracting a contiguous number field such as.                 8908078               89670890                    .                  .                    .                23019                     .                e.t.c.     P.S.: I would be grateful to feedback on how this is achieved in 'sed'.        I know how to do this with 'grep' and 'awk'       I would like to find out if my - albeit limited - understanding of        'sed' has holes in it and if there is way to do this in 'sed' which I       have simply overlooked.
65627
It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.  The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.  touch sample.txt find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;   The expected output is:     ./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT   Instead it produces:     ./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT   (the example has been tested also with old-school command substitution through backquotes '`' with the same result)  What am I doing wrong?
91376
Is this possible? I read somewhere that the following command would do it:     sed -e [command] [file]   but it appeared to do the same thing as just     sed [command] [file]   (it did not save the changes). Is there any way to do this using sed?
97125
I have the following string: /tmp/test/folder1/test.txt  I wish to use sed to substitute / for \/ - for example:  \/tmp\/test\/folder1\/test.txt   So I issue:  echo "/tmp/test/folder1/test.txt" | sed "s/\//\\\\//g"   Although it returns:     sed: -e expression #1, char 9: unknown option to `s'   I am escaping the forward slash and backslash - so not sure where I have gone wrong here
104748
Right now I'm using echo &quot;Hello World&quot; &gt;&gt; file.txt  to append some text to a file but I also need to add text below a certain string let's say [option], is it possible with sed? EG: Input file Some text Random [option] Some stuff  Output file Some text Random [option] *inserted text* Some stuff
21314
I have this output as a result of comparing files with diff:  &lt; IF-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 260 --- &gt; IF-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 25   I need this output:  lns-wall-01-t2 old:260 new:25   I want to use sed.
88182
sed 's/[long1][long2]/[long3][long4]/' file.txt   I would like to split this command onto multiple lines - f.x. something like this:  sed 's/     [long1]     [long2]   /     [long3]     [long4]   /' file.txt   Using \ or separating strings didn't work.
61269
Is there a way to modify a file without writing the contents to another file, without sed and awk?  For example:  $ cat test.txt aaa aaa bbb ccc ddd   Replacing using sed with -i option, sed -i 's/aaa/NNN/g' test.txt will produce the following:  NNN NNN bbb ccc ddd   How to do that without awk and sed?
121845
inputs:  line1 with the PATTERN that contains ( )  line2 with the PATTERN that contains ( )  lineN with the PATTERN that contains ( )    outputs:  line1 with the PATTERN that contains ( ) ; line2 with the PATTERN that contains ( ) ; ... lineN with the PATTERN that contains ( ) ;   I tried this:  find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"   but it didn't work.
48946
Due to an application bug as yet undiagnosed, I have several hundred servers with a full disk.  There is one file that has been filled up with duplicate lines—not a log file, but a user environment file with variable definitions (so I can't just delete the file).  I wrote a simple sed command to check for the erroneously added lines and delete them, and tested it on a local copy of the file.  It worked as intended.  However, when I tried it on the server with the full disk, I got approximately the following error (it's from memory, not copy and paste):  sed: couldn't flush /path/to/file/sed8923ABC: No space left on deviceServerHostname   Of course, I know there's no space left.  That's why I'm trying to delete stuff!  (The sed command I'm using will reduce a 4000+ line file to about 90 lines.)  My sed command is just sed -i '/myregex/d' /path/to/file/filename  Is there a way I can apply this command despite the full disk?  (It must be automated, since I need to apply it to several hundred servers as a quick-fix.)  (Obviously the application bug needs to be diagnosed, but in the meantime the servers aren't working correctly....)    Update: The situation I faced was resolved by deleting something else that I found out I could delete, but I'd still like the answer to this question, which would be helpful in the future and for other people.  /tmp is a no-go; it's on the same filesystem.  Before I freed up disk space, I did test and find out that I could delete the lines in vi by opening the file and running :g/myregex/d and then successfully save the changes with :wq.  It seems it should be possible to automate this, without resorting to a separate filesystem to hold a temp file....  (?)