I know how to edit a file using sed one step at a time, but how would I create an actual sed script and then use it on the file to do the same things that the individual sed commands would do? For example these 3 sed commands:  sed -i '4i\ ' baconFile sed -i 's/,/\t\t/' baconFile sed -i 's/,/ /' baconFile
123973
Typing /usr/bin/env sed -f in terminal works.  But if use it as a shebang,   #!/usr/bin/env sed -f  s/a/b/   The script will be fail to execute:  /usr/bin/env: sed -f: No such file or directory   I kind of believe that it's related with the -f. But how to resolve this?
78550
I have a format looking like this: 983243 woiewewrsadhjf 234 093111 srewdslkjfdgdsdsf 111  Can i use sed to search for all instances of text in the third field and change only those while leaving other third fields as is? So searching for 111 in the third field, changing all third fields containing 111 into othertext, wile leaving other third fields (such as 234) as they are. If possible can you guys break down the solution so I can get educated on how the command is formed. I am here to learn.
97449
I want to write the Ubuntu analogue of a "batch file" (a shell script). But I don't know how to use chmod +x filename command to make it so that the script can be run. Nor do I know where to use it.
105385
I created the following script:  #!bin/bash cat &gt; Top10 &lt;&lt;EOF Linux Mint 17.2 Ubuntu 15.10 Debian GNU/Linux 8.2 Mageria 5 Fedora 23 openSUSE Leap 42.1 Arch Linux CentOS 7.2-1511 PCLinuxOS 2014.12 Slackware Linux 14.1 FreeBSD EOF sed -ri "s/^[^0-9]*$//" Top10 sed -r "s/(.*)([[:space:]][[:digit:]]*.*)$/\2\1/" Top10 | sed -r "s/([[:space:]])([[:digit:]])/\2/" | sed -r "s/([[:digit:]])([[:alpha:]])/\1 \2/" &gt; Top10 sed -r -i "s/(.*)/\L\1/" Top10 sed -r -i "y/[aeiou]/[AEIOU]/" Top10 sort Top10 -g -o Top10 cat Top10   When I run it a few times the following happens:     As you can see sometimes the Top10 file turns out empty and sometimes it turn out the way I needed it to be. I know that the command which replaces the extensions from the end to the front of a line is done poorly. I ran this script on a VMware virtual machine. Could that be the reason?
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....  (?)
122203
I have a Bash script that writes data into a log file, then from that I take out stuff I want using the sed command. That gives me a file data.txt. What I want to know is how I go about deleting specific line inside that data.txt file.  For example:  123 456 789   I want to remove the 2nd line, containing 456 so that I only have  123  789   I tried with sed '2d' data.txt but it doesn't work.  I use this command to create data.txt from log.log:  sed -nE '1s/.{1}(.{2}).*/\1/p;' log.log &gt;&gt; data.txt
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/::'`
60541
So I've been using 'sed' on linux for a while, but have had a bit of difficulty trying to use it on OSX since 'POSIX sed' and 'GNU sed' have so many little differences.  Currently I'm struggling with how to insert a line of text after a certain line number. (in this case, line 4)  On linux I would do something like this:  sed --in-place "4 a\  mode '0755'" file.txt   So on OSX I tried this:  sed -i "" "4 a\ mode '0755'" file.txt   However this keeps giving me a 'extra characters after \ at the end of a command' error.  Any ideas what's wrong here?  Do I have a typo?  Or am I not understanding another difference between versions of sed?
94078
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 "/REPLACETHIS/r temp.TXT" -e "s///" devel.txt;   This inserts the content after the string, and then deletes the string afterwards.  Is this the best way to do it?
5286
I have a script that reads a text stream and generates a file of sed commands that is later run with sed -f. The generated sed commands are like:  s/cid:image002\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1922/g s/cid:image003\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1923/g s/cid:image004\.jpg@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1924/g   Assume the script which generates the sed commands is something like:  while read cid fileid do     cidpat="$(echo $cid | sed -e s/\\./\\\\./g)"     echo 's/'"$cidpat"'/https:\/\/mysite.com\/files\/'"$fileid"'/g' &gt;&gt; sedscr done   How can I improve the script to ensure all regex metacharacters in the cid string are escaped and interpolated properly?
45974
When using sed to replace strings in-place, is there a way to make it report the changes it does (without relying on a diff of old and new files)?  For instance, how can I change the command line  find . -type f | xargs sed -i 's/abc/def/g'   so I can see the changes that are made on the fly?
68545
A related question is .  I often have to edit a large file by removing a few lines from the middle of it. I know which lines I wish to remove and I typically do the following:  sed "linenum1,linenum2 d" input.txt &gt; input.temp   or in-line by adding the -i option. Since I know the line numbers, is there a command to avoid stream-editing and just remove the particular lines? input.txt can be as large as 50 GB.
73286
I have a file that contains multiple lines of xml. I would like to replace certain parts of the file. Some parts of the file contains quotation marks (") which I would like to replace. I have been trying to escape the quotation mark with \, but I don't think this is working based on the result of my file.  Here is an example of one of my sed commands:  sed -e "s/\"text\"/'text'/ig" file.xml &gt; temp.tmp   Is this how you escape quotation marks in a sed command or am I doing something wrong?
31972
Suppose I have a file that contains multiple occurrences of both StringA and StringB. I want to replace all occurrences of StringA with StringB, and (simultaneously) all occurrences of StringB with StringA.  Right now, I'm doing something like  cat file.txt | sed 's/StringB/StringC/g' | sed 's/StringA/StringB/g' | sed 's/StringC/StringA/g'   The problem with this approach is that it assumes StringC doesn't occur in the file. While this isn't an issue in practice, this solution still feels dirty -- that is, it feels like an opportunity to learn more unix magic. :)
72768
I'm trying to execute this command  sed -i -e "s/BASE_64/$BASE_64/" FILE_NAME   where $BASE_64 is a base 64 representation of a file content.  sed gives me an error since the string is too long.     Argument list too long   How is it possible to avoid this error?
65206
Imagine an output of a command like  44444 55555 11111 22222 33333   how can I yank the first N lines (first two in the example above) and append them at the end, but without using temp file (so only using pipes)?  11111 22222 33333 44444 55555   Something along the lines of | sed -n '3,5p;1,2p' (which obviously doesn't work as sed doesn't care about the order of the commands).
22924
How can I search and replace text that contains / with sed?  I currently use the following command which doesn't work  sed -i "s/queue_directory = /var/spool/postfix-secondary/queue_directory = /var/spool/postfix-$newnumber/g" /etc/postfix-$newnumber/main.cf