Baba
Baba

Reputation: 35

Linux shell script to search and delete line with respect to todays date with format mm/dd/yyy

Linux shell script to search and delete line with respect to todays date with format mm/dd/yyy.

Example: to be removed 11/08/2016

Input :

joba_name_11   11/02/2016 06:30:01  -----                RU 5359761/1
joba_name_11   11/07/2016 06:30:01  -----                RU 5359761/1
joba_name_11   11/08/2016 06:30:01  -----                RU 5359761/1

Output :

joba_name_11   11/02/2016 06:30:01  -----                RU 5359761/1
joba_name_11   11/07/2016 06:30:01  -----                RU 5359761/1

Syntax used :

$TODAY = 11/08/2016
sed -i 's/$TODAY/d' < $inputfile  > $outputfile

Error:

sed: -e expression #1, char 3: unknown command: `/'

Upvotes: 0

Views: 55

Answers (2)

Ko Cour
Ko Cour

Reputation: 933

Assuming the use of sed is essential:

TODAY=$(date +"%m/%d/%Y")
sed "\|$TODAY|d" inputfile > outputfile

Note that it says the same as /pattern/d, but because the $TODAY contains slashes, its better to choose a different separator - in this case |.

Upvotes: 0

Janek
Janek

Reputation: 3242

Grep to the rescue - use grep -v to show all lines not containing a pattern. Combined with date (with format %m/%d/%Y to get your date format) you get a oneliner:

grep -v `date +%m/%d/%Y` < infile > outfile

(Backticks in bash are used to execute a program, in this case you're supplying grep with the output of date.)

Upvotes: 1

Related Questions