[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

grep a file by date

Posted on 2011-10-03
8
Medium Priority
?
734 Views
Last Modified: 2013-11-22
Hi,
How to grep a file by its date as filename. For example test_20111003114318.txt. I tried something like below

egrep -v "^$|[/]$" test_20111003114318.txt

But I don't want to hard code the name as above , because I need to go and change it everyday..
0
Comment
Question by:new_perl_user
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904888
you could make a bash script which takes the date as an argument and then implements it into the command?
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904943
Save as dgrep and run ./dgrep 20111003114318 to find the file. I believe the script is correct, i will test it when i get my linux machine online
#! /bin/bash
# run egrep on date
egrep -v "^$|[/]$" test_$1.txt

Open in new window

0
 

Author Comment

by:new_perl_user
ID: 36904948
can I implement in the above command.

something like  egrep -v "^$|[/]$" test_$date.txt
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:themrrobert
ID: 36905047
My solutions is to create a new file, dgrep in your current working directory. Copy/paste my code into this file, and then run chmod +x dgrep

Then you can run ./dgrep 2020204083 and it will run the search.

$1 is the reference to the first command line argument passed to the script, the name is not arbitrary
0
 

Author Comment

by:new_perl_user
ID: 36905068
what if I automate the process rather than running it from command line
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36905106
Do you need to grep filenames with specific timestamps or can you glob it?  If you need specific dates, how do you determine which dates need to be passed to the script?
0
 

Author Comment

by:new_perl_user
ID: 36905131
it always will  be the sysdate. No worries about time. If it will able to pick up file by sysdate then it should be fine.
0
 
LVL 21

Accepted Solution

by:
Papertrip earned 2000 total points
ID: 36905181
egrep -v "^$|[/]$" test_`date +%Y%m%d%H%M%S`.txt

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question