Evaluate part of a text line

IttayD

I have an expression of the form @(<date calculation>) that I want to replace with the result of date +%s --date "now<date calculation>". So for example @(-1 day) would be replaced with the result of date +%s --date "now -1 day".

The expression is embedded in a line of text and I can have several. For example echo hi @(-1 day) bye "@(-1 hour)". The result of evaluation should be something like echo hi 1491848561 bye "1491931365". So I want just the expression to be evaluated, but nothing else.

I tried using GNU's sed 'e' command: sed -r 's|@\(([^)]*)\)|date +%s --date "now\1"|e', but this evaluates the entire line, not just the replaced expression.

I'm not attached to using sed, so any other suggestion is welcome, but I'm curious on how to make sed work, for general knowledge

steeldriver

As far as I know, the GNU sed e modifier always passes the whole pattern space to the shell - and that shell is always /bin/sh

You may have more luck using perl - for example

$ echo 'For example echo hi @(-1 day) bye "@(-1 hour)"' | 
    perl -pe 's/@\((.*?)\)/sprintf "%s", $d = `date +%s --date "now $1"`, chomp $d/ge'
For example echo hi 1491874845 bye "1491957645"

(unlike sed, the perl e modifier appears not to remove the trailing newline from the shell command's output - hence the chomp).

However if you are going to use perl it is likely preferable to make use of one of the available modules to do the date manipulation - for example

$ echo 'For example echo hi @(-1 day) bye "@(-1 hour)"' | 
    perl -MDate::Manip=ParseDateDelta,UnixDate -pe 's/@\((.*?)\)/UnixDate(ParseDateDelta($1), "%s")/ge'
For example echo hi 1491875065 bye "1491957865"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

BASH read a line but evaluate just a part

Bash, evaluate textual value and remove it's first part in one line

Read text file - and export part of text line

Replace part of line with different text from another file line by line

how to display part of a text in a straight line?

Move a specific part of text to next line if it have to

sed new lines with \n as part of the line text

Remove the first part of each line of a text file

Get as string a part of an line from a text

Replace a certain part of a text in a line - python

How to extract specific part of a line in a text in Python

PowerShell: In a text file, make this line part of the previous line

To modify a line that match start and mid part of a text in a line inplace

Find portion of text in a line, then add it to another part of the same line

Evaluate keras model on part of output

evaluate numeric part of a "sym" variable

How to read a specific line in a text file and return a part of it?

bash: How to replace an entire line in a text file by a part of its content

How to sort lines of text alphabetically based on a part of each line?

How to export each part of a line of text file to its own file?

Using part of the first line of the text file as the key in RDD

python - extract a part of each line from text file

Adding text to input, only adds second part to last line

Replace one part of a specific line in a text file in python

Edit part of a line in text without losing other lines

Linux get part of line's text if matched string in files

Rewriting array to text file, need to put each part on new line

How can i change part of richTextBox line text?

vim yanked the entire line, but paste the text part later between texts?