LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2019, 06:25 AM   #1
Kgeil
Member
 
Registered: Mar 2014
Posts: 32

Rep: Reputation: Disabled
Sed script within bash script produces unexpecte EOF error


Hi, I have a sed script which runs just fine at the command prompt, but when I include it in a bash script, it fails with: unexpected EOF while looking for matching `"' As always, any help or suggestions are greatly appreciated. It might take me an hour or two to reply to comments, thank you.

I'm working in Ubuntu 16.04

The script will eventually change two patterns within a log line (the combined sed command is commented out). The sed line is:


Code:
sed -i -r "s#INIT\][0-9]{4}\s[A-Z][a-z]{2}\s[0-9]+#INIT]$ymdDate#g" $1
The script is as follows:

Code:
#!/bin/bash

if [ "$1" = "" ]; then
    echo "ERROR: Please enter filename '\n' Example: change-ossec-alertDate.sh myalerts.log "
else
#today= `date "+%s" | cut -c1-6`
today=$(date "+%s" | cut -c1-6)
ymdDate=$(date "%Y %b %d)

echo "today = $today"
# the first part of the sed command below (before the ";") works fine
# sed -i -r  "s#AV - Alert - \"[0-9]{6}#AV - Alert - \"$today#g;s#INIT\][0-9]{4}\s[A-Z][a-z]{2}\s[0-9]+#INIT]$ymdDate#g" $1

# the line below works at the prompt, but fails in a script
sed -i -r "s#INIT\][0-9]{4}\s[A-Z][a-z]{2}\s[0-9]+#INIT]$ymdDate#g" $1


fi


And last but not lease, here is the sample text:

Code:
AV - Alert - "1557833239" --> RID: "110021"; RL: "2"; RG: "windows,applocker,"; RC: "Applocker audit mode program would have been blocked"; USER: "None"; SRCIP: "None"; HOSTNAME: "(DC1) 10.101.4.210->WinEvtLog"; LOCATION: "(DC1) 10.101.4.210->WinEvtLog"; EVENT: "[INIT]2019 May 14 07:53:51 WinEvtLog: Microsoft-Windows-AppLocker/EXE and DLL: Warning(8003): no source: KevinAdmin: TESTDOMAIN: 2012R2-DC1.testdomain.local: %SYSTEM32%\MMC.EXE was allowed to run but would have been prevented from running if the AppLocker policy were enforced.[END]";

Thanks again,


Kevin

Last edited by Kgeil; 05-15-2019 at 06:26 AM.
 
Old 05-15-2019, 06:39 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,699

Rep: Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981
Code:
ymdDate=$(date "%Y %b %d)
this line is just wrong
 
Old 05-15-2019, 07:04 AM   #3
Kgeil
Member
 
Registered: Mar 2014
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
ymdDate=$(date "%Y %b %d)
this line is just wrong
If you could share why you make that statement, I would appreciate it.

Thanks
 
Old 05-15-2019, 07:39 AM   #4
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,031

Rep: Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788Reputation: 788
Quote:
Originally Posted by Kgeil View Post
If you could share why you make that statement, I would appreciate it.
The line has an unbalanced quote character.

After adding the missing second quote, your EOF error will turn in an error issued by the date command. What precise error will be issued probably depends on where you will put your second quote. You may want to re-read the manual page for date.

Last edited by berndbausch; 05-15-2019 at 07:41 AM.
 
1 members found this post helpful.
Old 05-15-2019, 08:31 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,699

Rep: Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981Reputation: 3981
I suggest you to use shellcheck.net to check your script. That will help you to solve your issues (except the date format - see man date).
 
1 members found this post helpful.
Old 05-15-2019, 08:55 AM   #6
Kgeil
Member
 
Registered: Mar 2014
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks for the great advice from both of you! shellcheck.net is totally awesome. Of course, my date formatting was an issue, but my biggest issue was not really thinking through what the EOF error was telling me. I was focused on the wrong line of code the whole time...

Thanks again,
Kevin

My new script is below. Advice is still appreciated, but it does work as intended.

Code:
#!/bin/bash

if [ "$1" = "" ]; then
    printf 'ERROR: Please enter filename \n Example: change-ossec-alertDate.sh myalerts.log '
else

today=$(date "+%s" | cut -c1-6)
ymdDate=$(date '+%Y %b %d')

echo "ymdDate = $ymdDate"

echo "today = $today"

sed -i -r  "s#AV - Alert - "[0-9]{6}#AV - Alert - "$today#g;s#INIT\][0-9]{4}\s[A-Z][a-z]{2}\s[0-9]+#INIT]$ymdDate#g" "$1"



fi
 
Old 05-15-2019, 08:59 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,963
Blog Entries: 3

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
You can try adding the following as the second line of the script:

Code:
set -x
That will give you a preview of each line exactly as the script plans to execute. Then you can see how what it actually produces diverges from what you expected it to do.
 
1 members found this post helpful.
Old 05-15-2019, 09:34 AM   #8
Kgeil
Member
 
Registered: Mar 2014
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
You can try adding the following as the second line of the script:

Code:
set -x
That will give you a preview of each line exactly as the script plans to execute. Then you can see how what it actually produces diverges from what you expected it to do.
Thanks. That's another awesome debugging tool!
 
Old 05-16-2019, 12:41 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,642

Rep: Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481Reputation: 2481
I usually use 'set -xv' YMMV
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 10:38 AM
[SOLVED] Bash Scripting Error: EOF encountered in a comment? dilzniksan Programming 4 06-02-2010 11:55 AM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration