I want to comment out a line (add a # to the beginning). I used this command: sed -i '96s/^/#&amp;/' file.txt on Linux to insert comment at line # 96.  If the line number is a variable, then how can I use this command?  I have tried these commands:  (1)  sed -i '${line_number}s|^|#&amp;|' file.txt   (2)  sed -i '${line_number}s/^/#&amp;/' file.txt   But they don't work. How can I do this?
4793
I want to do:  cat update_via_sed.sh | sed 's/old_name/new_name/' &gt; 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 tried using them but the substitution doesn't happen (no error). I have tried:  cat update_via_sed.sh | sed 's/old_run/new_run/' cat update_via_sed.sh | sed 's/$old_run/$new_run/' cat update_via_sed.sh | sed 's/${old_run}/${new_run}/'
44642
Is there an issue with sed and new line character? I have a file test.txt with the following contents    aaaaa   bbbbb   ccccc   ddddd     The following does not work: sed -r -i 's/\n/,/g' test.txt    I know that I can use tr for this but my question is why it seems not possible with sed.     If this is a side effect of processing the file line by line I would be interested in why this happens. I think grep removes new lines. Does sed do the same?
122795
I have a requirement like i have to remove the strings or numbers between two Parenthesis in a file. I used sed command but it is working on a single line. My opening Parenthesis is in one line and the closing parenthesis is in other line. How can i do it?  I tried this command using sed:  sed -e 's/([^()]*)//g'   but this works only when My opening and closing parenthesis are on the same line.  For Example:-  Input file:  select a ,b ,c FROM ABCD (select e ,f ,g ,h FROM XYZ)   Output should be:  select a ,b ,c FROM ABCD
57883
I've just come across the &lt;&lt; command, used like so:  cat &gt; outfile.txt &lt;&lt;EOF Multi-line content that will be written to outfile.txt EOF   Now, I've no idea what this is called, but I'd quite like to know it's name, primarily so I can go and search and find out more about its syntax. Sadly, Googling for "&lt;&lt;" just doesn't work.
21825
I have a code block as follows in a file:  BalancerMember http://snode1:80 min=1 max=1000 loadfactor=1  timeout=30 keepalive=on route=tc_snode1 BalancerMember http://snode2:80 min=1 max=1000 loadfactor=1  timeout=30 keepalive=on route=tc_snode2   I want to search for all lines which had tc_ keyword and add status=D at the end of those lines. So, the above entries will become like this  BalancerMember http://snode1:80 min=1 max=1000 loadfactor=1  timeout=30 keepalive=on route=tc_snode1 status=D BalancerMember http://snode2:80 min=1 max=1000 loadfactor=1  timeout=30 keepalive=on route=tc_snode2 status=D   I am using the following command to achieve this:  sed -i '/tc_/a status=D' app.conf   But I can see status=D is being after a newline. I don't want a newline for this. It should be appended with a '' (space) .
96079
I am using the following code to read specific lines in an ASCII file "text.dat"  sed -n '/Columnaxis/p' text.dat   The output is   Columnaxis: 100   In order to output only the second part previous line, I added the following to the previous command line:   sed -n '/Columnaxis/p' text.dat | awk '{ print $2 }'   The output of the last command is "100"  How can I update the last code to output negative numbers, regardless of the number's original sign? ( i.e if the input is "+100" I want the code to output "-100" also if the input is "-100", I want the code to output "-100"  PS. The content of text.dat is something like the following :  Columnaxis: 100 Columnaxis_1: 100 Columnaxis_2: -100 Columnaxis_3: 50 Columnaxis_4: -2.3 Columnaxis_5: 1.2 Columnaxis_6: -5 Columnaxis_7: -2 Columnaxis_8: -4 Columnaxis_9: -2   I want the code to output the following:  Columnaxis: -100   Let's say that the input has "Columnaxis: +100", then I want it to be  Columnaxis: -100
104690
I want to sed using a variable.  For example, sed -n '1,10p' file. So, instead of 10, I want to use $k where k is an integer.
87030
I am doing grep and sed and I get 2 lines of a file that I am interested in. How can I get these lines in a single line ending with the new line character? Now I am getting:    pattern1   pattern2     I would like to get pattern1 pattern2 \n
122379
I am trying to write a script that will delete all comments and everything in between inside C files in my current directory. I've been using sed, and this is what I have so far:   sed -i '/ * [^()] */d' *.c  This works when the comments are on the same line as an asterisk or backslash.   However it doesn't work when there is a commented line without a slash or asterisk.   I know sed goes line by line, I just don't know how to tell it to keep deleting until it sees a */.
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).
109072
This is more a conceptual thing than a current problem.  Would it be possible to do this:  tail -f &lt;file&gt; | grep "string" | sed 's/stuff//g' &gt;&gt; output.txt  The problem I'm thinking is that tail -f never terminates so it won't progress in the command. I'm trying to read and output a file in real-time. If this doesn't work how would I do 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?
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
116749
I have been told it was possible, but without a single working example, that sed can read from a string variable without need for an input file. I have yet to get it to work.  For general safety, I’m writing the $PATH variable to another variable, as I mess with this, because I don’t need other problems to arise until I know exactly how to do this.  Consider the following:  ~$x=$PATH ~$sed -i 's/:/ /g' $x   this fails with: No such file or directory.  Here are some others I have tried:  ~$ sed -i 's/:/ /g' | (read x)  sed: no input files ~$ sed -i 's/:/ /g' | (read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier sed: no input files ~$ sed -i 's/:/ /g' | $(read x)  sed: no input files ~$ sed -i 's/:/ /g' | $(read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier sed: no input files ~$ sed -i 's/:/ /g' &lt; $(read $x)  bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier bash: $(read $x): ambiguous redirect ~$ sed -i 's/:/ /g' &lt; read $x  bash: read: No such file or directory ~$ sed -i 's/:/ /g' &lt; (read x)  bash: syntax error near unexpected token `(' ~$ sed -i 's/:/ /g' &lt; $((read x))  bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: syntax error: operand expected (error token is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games") ~$ sed -i 's/:/ /g' &lt; $((read $x))  bash: read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: division by 0 (error token is "usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games") ~$ sed -i 's/:/ /g' &lt;&lt; $((read $x))  &gt; ;^C ~$ sed -i 's/:/ /g' &lt;&lt; $(read $x) &gt; ;^C ~$ sed -i 's/:/ /g' $x sed: can't read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory ~$ sed -i 's/:/ /g' &lt; $x bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory ~$ sed -i 's/:/ /g' &lt; echo $x bash: echo: No such file or directory ~$ sed -i 's/:/ /g' |  echo $x /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games sed: no input files   Can this even work? I would prefer not to need to write files I don't need just so I can use sed. For this particular example, if ~$x=$PATH ~$sed -i 's/:/ /g' $x actually worked the way I would have hoped, I would get:   /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games   which I could then assign to a variable, and use in future commands, like ls $x
98993
How do we extend a command to the next line? Basically whats the windows alternative for Linux's ls -l \ /usr/  Here we use backslashes to extend the command onto the next lines. What's the equivalent for Windows?
109492
How to display line number while doing grep on a file. For example:  grep CONFIG_PM_ADVANCED_DEBUG /boot/config-`uname -r
104986
I'm trying to run  sed -i "/$variable/c \$variable1' file.txt   But it's not displaying any change in the file: file.txt.  But the same command, with out the variables works fine:  sed -i "/variable/c \variable1' file.txt