I am creating a basic shell script to manipulate a database as a college assignment. However, How many times I try, I can't get the sed command fully  correct. The functionality where sed is to be used is this:  $name is the variable. User will supply the name via the terminal. This name will be looked up in the database and will be deleted if present.  The command I used is this:  sed -i /$name/d &lt;filename&gt;   However, this command only works if the entered value contains no space.  I tried out my solution by using what I could salvage from  such as sed -i '/$name/d' &lt;filename&gt;  However, to no avail. How to go about this?
51572
I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script:  cat shared.txt sed 's/whatever1/$2 ... whatever2/' &gt; shared2.txt   The result in shared2.txt looks like:  ...$2....   What did I do wrong?
72476
My current best bet is:  for i in $(find . -name *.jpg); do echo $i; done   Problem: does not handle spaces in filenames.   Note: I would also love a graphical way of doing this, such as the "tree" command.
81308
I want to run a command such as this in a bash script:  freebcp &lt;authentication and other parameters&gt; -t "," -r "\r\n"   When run directly on the command line, the command works as expected. But when placed in a variable in a bash script it returns an error such as this:  Msg 20104, Level 3 Unexpected EOF encountered in bcp datafile  Msg 20074, Level 11 Attempt to bulk copy an oversized row to the server  bcp copy in failed   When command is placed in a variable and double quotes are escaped:  cmd="freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t \",\" -r \"\r\n\" -c" `$cmd`   Note: Putting the following in the script works as expected:  `freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t "," -r "\r\n" -c`   So I know there's some quoting/escaping/expansion problems but I can't figure out how to fix it.  Note 2: Single quoting -t -r parameters doesn't work either
72600
Recently it seems that shell has changed how it handles spaces in parameters. It used to handle them properly (Or at least as I intended it to), but now it recognises it as a bunch of different parameters and completely misses the escape character.  An example of a small script that used to work very well:  #!/bin/sh  exec /home/evan/.applications/Sublime\ Text\ 2/sublime_text $1   So I would give it some filepath with spaces and it used to open the proper file eg, /home/evan/Document/My\ File\ Path/file.txt would open that file.txt. Now it opens "My", "File", "Path", and some unknown "file.txt" Obviously, that won't be the file.txt that I am looking for.  Any help would be appreciated. Thank you in advance.  OS: Ubuntu 13.04 GNOME edition with latest updates  EDIT: I forgot to mention that the command executes perfect. The parameter does not.
6438
In my bash script I have an external (received from user) string, which I should use in sed pattern.  REPLACE="&lt;funny characters here&gt;" sed "s/KEYWORD/$REPLACE/g"   How can I escape the $REPLACE string so it would be safely accepted by sed as a literal replacement?  NOTE: The KEYWORD is a dumb substring with no matches etc. It is not supplied by user.
87431
I use sed -n '/string/,$p' file &gt;&gt; otherfile to copy text from a line with certain string to another file.  Now I want the copied text to be deleted in the original file. I tried different methods with sed and awk but nothing worked.  How to use sed to delete the text of a file from the beginning line with a certain string in it?  texta  texta1  textb    &lt;- string, delete rest of the text from here  textb1  textc  textc1   or would it be easier to cut instead of copying with sed and what would be the command instead?
17165
I'm trying to use sed to edit a config file. There are a few lines I'd like to change. I know that under Linux sed -i allows for in place edits but it requires you save to a backup file. However I would like to avoid having multiple backup files and make all my in place changes at once.  Is there a way to do so with sed -i or is there a better alternative?
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
112882
Something like the following is what I what I'm after,  but my code doesn't work, no matter how I escape {} and + ;     find ./ -maxdepth 1 -type d -name '.*' -exec \     find {} -maxdepth 1 -type f -name '*.ini' -exec \         md5sum \{\} \\; \;   After seeing this ,  I found that the following code works, but it isn't nesting find as such, and I suspect there is a better way to do this particular job.       find ./ -maxdepth 1 -type d -name '.*' \ -exec bash -c 'for x; do     find "$x" -maxdepth 1 -type f -name "*.ini" \     -exec md5sum \{\} \;; \ done' _ {} \+   Is there some way to nest find -exec without the need to invoke  a shell (as above), with all its whacky quoteing and escape constraints?    Or can this be done directly in a single find command,  using a blend of its many parameters?
21864
I've been using sed for quite some time but here is a quirk I came around with, which I am not able to resolve.   Let me explain my problem with the actual case.    Scene#1  printf "ls" | xclip -selection clipboard echo "ls" | xclip -selection clipboard   In the first command, I pipe printf output to xclip so that it gets copied to the clipboard. Now, printf, unlike echo does not insert a new line at the end by default. So, if I paste this content into terminal, the ls command that is copied does not automatically run.   In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.   This is undesirable for me. So, I wanted to remove the newline using sed, but it failed, as explained in the scene below.  Scene#2  echo "ls" | sed -r 's/\n//g' | xclip -selection clipboard   The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.   I also tried removing carriage return character \r. But nada. It seems I am missing something very crucial/basic here.
37813
I want to split 'hello' into h e l l o in an array using only bash, I could do it in sed with sed 's/./&amp; /g' but I want to know how to split a string into an array in Bash when I do not know what the delimiter would be, or the delimiter is any single character.  I don't think I can use ${i// /} without some creativity because the delimiter is an unknown, and I don't think that expression accepts regex.  I tried using BASH_REMATCH with [[ string =~ ([a-z].).* ]] but it doesn't work as I expected.  What is the proper way to use only bash to accomplish a string.split() type of behavior?  The reason is that I am trying to write the rev utility in all bash:    while read data; do   word=($(echo $data|tr ' ' '_'|sed 's/./&amp; /g'))   new=()   i=$((${#word[@]} - 1))   while [[ $i -ge 0 ]]; do     new+=(${word[$i]})     (( i-- ))   done   echo ${new[@]}|tr -d ' '|tr '_' ' '   done   But I used tr and sed, I want to know how to do the split properly and then I will fix it to be all bash.  Just for fun.
42698
That is literal, {fd} isn't a placeholder. I have a script that does this, and does not source in anything, nor does it reference {fd} anywhere else.  Is this valid bash?  exec {fd}&lt;/dev/watchdog
95497
I saw sed 's=.*/==' in context of sh script and I'm puzzled. I could not find in sed manual or web search (for sed s=) how s is used, not s///. Apart from s I see only one potential command here = (Print the current input line number), but in such case what the rest is doing...   Running the command in shell produces same output as input for e.g echo 'jkfdsa=335r34',  whereas echo 'jkfdsa=335r34' | sed 's/=.*/==/' does replacement as per manual. Also slightly modifying command to e.g. echo 'jkfdsa=3' | sed 's798=.*/==/' gives sed: -e expression #1, char 11: unterminated 's' command, so original should have some correct meaning. What is it?
34744
I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable way, but with sed I see no way to do that.  Part of my website uses generated static HTML files, and this sed line inserts correct DOCTYPE after each regeneration:  sed -i '1s/^/&lt;!DOCTYPE html&gt; \n/' ${file_name.html}   It works with GNU sed on Linux, but FreeBSD sed expects the first argument after -i option to be extension for backup copy. This is how it would look like:  sed -i '' '1s/^/&lt;!DOCTYPE html&gt; \n/' ${file_name.html}   However, GNU sed in turn expects the expression to follow immediately after -i. (It also requires fixes with newline handling, but that's already answered in )  Of course I can include this change in my server copy of the script, but that would mess i.e. my use of VCS for versioning. Is there a way to achieve this with sed in a fully portable way?
110915
&gt; can do it.  echo "text" &gt; file   tee can do it.  echo "test" | tee file    Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without using either &gt; or tee?
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
48283
I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts.  Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.        From the text book (with minor formatting changes):     There are some special variables in addition to the ones you set. You   can pass arguments to your script:  #!/bin/bash echo "Hello $1"       A dollar sign followed by a number N corresponds to the Nth argument   passed to the script. If you call the example above with ./test.sh the   output will be Hello Linux. The $0 variable contains the name of the   script itself.      After a program runs, be it a binary or a script, it returns an exit   code which is an integer between 0 and 255. You can test this through   the $? variable to see if the previous command completed successfully.     I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 -- I just don't get it.    Any help would be much appreciated.