Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Issue when doing a loop


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Issue when doing a loop

Hi,

I just have started learning shell scripting (sh). Why do i only get the date?

Code:
while read dt
do
   echo "Date : ${dt}
   sed -n '/${dt}/,/${dt}/p' file1.log | grep -w ERROR
done < date1.dat

INPUT - date1.dat
Code:
2019-04-05 04:58:25
2019-04-05 04:58:26
2019-04-05 05:00:56
2019-04-05 05:08:26
2019-04-05 05:14:08
2019-04-05 05:21:26
2019-04-05 05:31:08

Desired Output
Code:
Date : 2019-04-05 04:58:25
2019-04-05 04:58:25,696 [Timer-7] ERROR :  421 Exceeded allowable connection time, disconnecting.

Actual Output
Code:
Date : 22019-04-05 04:58:25
Date : 22019-04-05 04:58:26
Date : 22019-04-05 05:00:56
Date : 22019-04-05 05:08:26
Date : 22019-04-05 05:14:08
Date : 22019-04-05 05:21:26
Date : 22019-04-05 05:31:08

Moderator's Comments:
Mod Comment
Please wrap all code, file, input & output/errors in CODE tags
It makes it easier to read and preserves spacing for indenting code or fixed-width data

Last edited by rbatte1; 2 Weeks Ago at 07:09 AM..
# 2  
The sed does not find anything.
Your mistake is the 'ticks' around $dt, but the shell only expands $dt within "quotes".
Further, a /x/,/x/ range is always one line, so you can simply address it with one /x/.
Further, consider searching only at the beginning of the lines, by means of /^x/, here /^$dt/ or /^${dt}/
Further, you can save the invocation of grep.
Code:
sed -n "/ERROR/!d; /^$dt/p" file1.log

or
Code:
sed "/ERROR/!d; /^$dt/!d" file1.log

Note: in bash you need to turn off history substitution with comand set +H.
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Thank you MadeInGermany.
Do i always need to turn off history substitution?
# 4  
Only in an interactive bash shell (i.e. the command line), an exclamation mark followed by an alphanumeric character does history substitution.
For example
Code:
echo "!1"

prints line 1 from the command history.
Fortunately a bash script never does history substitution; a set +H work-around is not needed there.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Issue with while loop?
svajhala
Hi, I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a...... Shell Programming and Scripting
11
Shell Programming and Scripting
While Loop issue
onesuri
Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc... Shell Programming and Scripting
5
Shell Programming and Scripting
Issue with using While loop
funonnet
Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local...... Shell Programming and Scripting
8
Shell Programming and Scripting
loop issue
kojo
function ext { echo "THANKS & WELCOME BACK" } function upc { echo "TO EXPORT UPROC GIVE UPROC NAME PER LINE IN THE input.txt and PRESS Y" echo "TO GO BACK PRESS 99" read parm0 if ; then start elif ; then for i in `cat input.txt` ; do echo $i $UXEXE/uxext upr upr=$i...... Shell Programming and Scripting
0
Shell Programming and Scripting
loop issue
kkkk
I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below: host www184.domain.com { hardware ethernet 00:13:72:3B:B4:3A; fixed-address 192.168.0.184; }...... Shell Programming and Scripting
4
Shell Programming and Scripting

Featured Tech Videos