Find the answer to your Linux question:
Results 1 to 3 of 3
trying to do something very basic but having a very difficult time. assuming the following: Code: date | awk '{print "'\''"$2,$3"'\''"}' will return 'Oct 07' and Code: ls -ltr | ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    1

    1 Line script syntax problem

    trying to do something very basic but having a very difficult time.

    assuming the following:

    Code:
    date | awk '{print "'\''"$2,$3"'\''"}'
    will return
    'Oct 07'

    and

    Code:
    ls -ltr | grep `echo com`
    will return
    -rw------- 1 ??????? ????? 2440 Jul 06 19:15 commands.txt

    Why is it that I cannot combine them both to list and grep by 'Oct 07'?

    Code:
    ls -ltr | grep `date | awk '{print "'\''"$2,$3"'\''"}'`
    returns
    grep: 0652-033 Cannot open 7'.



    I just realized even if I did get this script's syntax correct it would not work as date returns Oct 7 and when doing ls -ltr the date is displayed as Oct 07

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    This is because the "date | awk ..." command returns two words -- Oct 07, so grep tries to grep the file "7" for the word "Oct". Perhaps if you put the date command in double-quotes
    Code:
    ls -ltr | grep "`date | awk '{print "'\''"$2,$3"'\''"}'`"
    I have no idea if that'll work, but you could give it a try....

    Welcome to the forums
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    If you are trying to list files/folders modified within less than 24 hours started from 00:00 today, try to use command find instead:
    find . -daystart -ctime -1 -exec ls -l {} \;

    Or if you still want to use ls and grep, try
    ls -ltr | grep "`date '+%b %d'`"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...