0

I am trying to read in a log file and create a new file with only the entries after the given date and time. For example I am looking for only after the time 07:00:00.000 in the below log:

2015-01-16 00:00:00.001 DATA
2015-01-16 07:05:00.121 DATA
2015-01-16 07:10:15:543 DATA

Would go to:

2015-01-16 07:05:00.121 DATA
2015-01-16 07:10:15:543 DATA

I can already get this to work with:

sed -n '/^2015-01-16 07/,$p' oglog.log > newlog.log

But when I try to add the date via a variable:

date=$(date -d -1hour +'%Y-%m-%d %H')

I cannot get it to work. I know you need to get the shell to expand variable via either double or single quotes (https://askubuntu.com/questions/76808/how-to-use-variables-in-sed-command), but with either of these methods it causes problems with $p in the replacement part of the sed command. How can I get the shell the expand one variable but not the other and use a command similar to below?

sed -n '/^$date/,$p' oglog.log > newlog.log
5
  • 1
    use double quotes in sed. Commented Jan 16, 2015 at 19:23
  • Right I have tried this method: result=$(sed -n "/^$testDate/,$p" logtest.log > newlogtest.log) returns: sed: -e expression #1, char 20: unexpected ',' and result=$(sed -n '/^"$testDate"/,$p' logtest.log > newlogtest.log) returns an empty file. Commented Jan 16, 2015 at 19:30
  • Note that if the first event after 07:00:00.000 is at 08:00:00.000, nothing will be printed. You might need to use awk which can do >= comparisons Commented Jan 16, 2015 at 19:31
  • @JonathanLeffler good to know! This log has 10's of entries every minute so I think it will be alright. Commented Jan 16, 2015 at 19:38
  • possible duplicate of Why is my sed command failing when using variables? Commented Jan 16, 2015 at 21:00

2 Answers 2

3
sed -n "/^$date/"',$p' oglog.log > newlog.log

You want the $date expanded by the shell, so that needs to be in double quotes. You don't want the $p expanded by the shell, so that needs to be in single quotes. You could also use a backslash to prevent the shell expanding that $p, but that quickly gets nasty if you have other meaningful backslashes in the regex:

sed -n "/^$date/,\$p" oglog.log > newlog.log
Sign up to request clarification or add additional context in comments.

Comments

1

Use

sed -n "/^$date/,\$p" oglog.log > newlog.log

variables are not expanded in single quotes.

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.