0

I have just recently got back into learning bash. Currently working on a project of mine and when using sed I've run into an issue, I've tried looking around the web for help but haven't had any joy. I suspect as I may not be using the correct terminology so I can't find what I'm looking for. ANYHOW.

So in my script I'm trying to assign the output of date to a variable. Here's the line from my script.

origdate=$(date)

When I call it the output looks like this:

Wed Oct  5 19:40:45 BST 2016

Part of my script then generates a file and writes information to it, part of which I am trying to use sed to find lines and replace parts of it. This is the first I've been playing around with sed, I've used it successfully so far for my needs. However I'm getting stuck when I try this:

sed -i '/origdate=empty/c\'$origdate'' $sd/pingcheck-email-$job.txt

When I run the script and it gets to this line, this is the error I'm getting:

sed: can't read Oct: No such file or directory
sed: can't read 5: No such file or directory
sed: can't read 19:52:56: No such file or directory
sed: can't read BST: No such file or directory
sed: can't read 2016: No such file or directory

I suspect it's something to do with the spaces in the date (variable), my question is: how can I work around this? Can I get sed to 'ignore' the spaces? or should I just use cut to cut the field for the date, and set that to a variable and the same thing again to set the time to another variable? Even if someone could kindly point me in the right direction that'd be great!

Thanks in advance!

3 Answers 3

1

double quote the variable

sed -i '/origdate=empty/c\'"$origdate"'' $sd/pingcheck-email-$job.txt

or alternatively, the whole script

sed -i "/origdate=empty/c\$origdate" $sd/pingcheck-email-$job.txt
1
  • The double quote worked a treat! Thanks - think I'll brush up on more sed functions etc anyhow when I get time but this helps with my needs for now - Many thanks!
    – keefer
    Oct 6, 2016 at 12:17
1

The problem is not with sed but rather with how bash word splits on your date given your command.

Bash

In bash, word splitting is performed on the command line so that text is broken up into a list of arguments. To illustrate, I'm going to run a simple script that outputs the first argument only.

bash -c 'echo $1' ignored_0 foo bar

Think of bash -c 'echo $1' ignored_0 as the command (sed in your case) and foo bar as the arguments. In this case, foo bar is split into two arguments, foo and bar.

To pass foo bar in as the first parameter, you need to have the text in either single or double quotes. See the GNU manual on quoting.

bash -c 'echo $1' ignored_0 'foo bar'
bash -c 'echo $1' ignored_0 "foo bar"

Parameter expansion does not occur when the variable is inside a single quote.

var="foo bar"
bash -c 'echo $1' ignored_0 '$var'
bash -c 'echo $1' ignored_0 "$var"

NOTE: In the command `bash -c 'echo $1', I do not want $1 to expand before being passed as an argument to bash because that's part of the code I want to execute.

Parameter expansion occurs when variables are outside of quotes, but word splitting will apply after the parameter is expanded. From the bash man page in the Word Splitting section:

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

From the GNU bash manual on Word Splitting:

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

var="foo bar"
bash -c 'echo $1' ignored_0 $var

The last step in Shell Expansions in Quote Removal where unquoted quote characters are removed before being passed to commands. The following command shows that ''"" has no effect on the arguments passed.

bash -c 'echo $1' ignored_0 foo''""

Application

In your example, the trailing '' after $origdate is extraneous. The important part is that $origdate is not quoted so word splitting applies to the expanded variable.

When -e is not passed to the sed command, sed expects the expression to be in one argument, or word from bash. When you run your command, your expression is /origdate=empty/c\Wed and the rest of the date is considered to be files for the expression to be applied to.

The simple fix is to put double quotes around the string for which you want to prevent word splitting. I've modified the command so that anyone can run this example without having the files on their system.

In this example, the \ must be escaped so that it is not considered an escape character for $.

echo "origdate=empty" | sed "/origdate=empty/c\\$origdate"

You can also change the type of quotes you are using without affecting word splitting like so.

echo "origdate=empty" | sed '/origdate=empty/c\'"$origdate"
2
  • Thank you so much for taking the time to respond! You gave me a far greater understanding to what was happening! Much appreciated!
    – keefer
    Oct 19, 2016 at 14:50
  • I did already up vote it however it says due to my reputation isn't high enough it isn't yet counted. I'll return when my reputation is high enough and do just that. Thanks again.
    – keefer
    Oct 24, 2016 at 12:40
0

You need escape by double slash \ / \%

1
  • Thanks for that - I've went with the double quotes for now but these answers have prompted me to brush up on sed and it's functions a bit more so I'll probably use this kind of method later in my project when I've more knowledge on how to do so. Many thanks for your help!
    – keefer
    Oct 6, 2016 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.