0

I am using the following code

      ff=`date +%h" "%Oe`
      pd=`date -d'yesterday' +%h" "%Oe`
      aa=`date -d'yesterday' +%d\/%m\/%Y`
      bb=`date +%d\/%m\/%Y`
      for j in `ls -lrt |egrep "$ff|$pd"|awk -F " " '{print $9}'`
      do
      sed -n "/${aa}/,/${bb}/p" ${j} 
      done

Logs from where I am fetching the data looks something like this

      [2015-01-07 18:39:18,212] host123 WARN com.host123 .elf.UserQuest -  
      Quest/option {o.q.more.paper.osc#0} references unknown dependent   
      {t.what.form.file.more.action} in application {src-code}. Please            
      revise.

      [2015-01-07 18:39:18,212] host123 WARN com.host123 .elf.UserQuest -   
      Quest/option {o.q.more.paper.osc#1} references unknown dependent 
      {t.what.form.file.more.action} in application {src-code}. Please  
      revise.

      [2015-01-07 18:40:34,281] cessor32 ERROR com.host123 
      .email.DirectMailer -  Unable to connect to server {1.1.1.1}:
      javax.mail.MessagingException: Could not connect to SMTP host:  
      1.1.1.1, port: 25, response: 451
      at              
      com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:996)
      at  
  com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
   at javax.mail.Service.connect(Service.java:233)
   at javax.mail.Service.connect(Service.java:134)
   at               
   com.host123.email.DirectMailer.deliverMessage(DirectMailer.java:191)

I am getting the following error after executing the script

   sed: -e expression #1, char 5: unknown command: `0'

Please do suggest something.

atul329
  • 67
  • 1
  • 3
  • 9
  • You should avoid using deprecated back tics, use parentheses like this: `ff=$(date +%h" "%Oe)` – Jotne Feb 25 '15 at 09:11
  • @Jotne Thanks for the advise. Can you please tell me the drawback of using back of using back tics in this **ff=`date +%h" "%Oe`** – atul329 Feb 25 '15 at 09:28
  • Do a google search or see her: http://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts – Jotne Feb 25 '15 at 11:12

3 Answers3

2

The issue here is that you're using the / delimiter in your sed command, while at the same time using patterns that contain the same character. Try changing your sed command to this:

sed -n "\#${aa}\#,\#${bb}\#p" "$j"

As NeronLeVelu has mentioned in the comments (thanks), it is necessary to escape the character with a backslash.

Alternatively, you could use awk to print your range of lines:

awk -v s="$aa" -v e="$bb" '$0 ~ s, $0 ~ e' "$j" 
Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • `\#${aa}#,\#${bb}#` normaly (posix) you need to escape the first delimiter in a pattern range (not like in a `s###` that take the first char as delimiter) meanwhile you got the point – NeronLeVelu Feb 25 '15 at 08:21
  • @NeronLeVelu While using this command `sed -n "\#${aa}#,\#${bb}#p" file.txt` or this `sed -n "\#${aa}\#,\#${bb}\#p" file.txt` i got the following error `sed: -e expression #1, char 30: unterminated address regex` – atul329 Feb 25 '15 at 09:45
  • try with double escape due to double quote `'\#` and `"\\#` – NeronLeVelu Feb 25 '15 at 10:59
  • @NeronLeVelu Tried using `"\\#` but it doesn't work getting the same exception again. `sed: -e expression #1, char 30: unterminated address regex`. – atul329 Feb 25 '15 at 11:30
  • 1
    could you show the content of aa and bb before the sed. Test on a AIX and a linux here works – NeronLeVelu Feb 25 '15 at 11:58
0

Tried making array out of ls -lrt |egrep "$ff|$pd"|awk -F " " '{print $9}' then loop through array?

sztyrymytyry
  • 151
  • 2
  • 12
  • Actually I have used this `" ls -lrt |egrep "$ff|$pd"|awk -F " " '{print $9}' "` before but never faced problem for the same. – atul329 Feb 25 '15 at 08:19
0

The problem is that value of bb contains /s (Your sed command also using / as delimiter) . Try changing the line to:

sed -n "#${aa}#,#${bb}#p" ${j}

Another suggestion is to use the newer format of ff=$(date +%h" "%Oe)

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27