3

I have a file where data is present in below format :

cat file.txt
02|123
04|321
08|453
09|364
01|789
23|765

so here 02,04 ... represents Hour and 123,321 ... represents some random value.

I want to delete all the lines from this file where first two value is less than the current hour which will get from date +'%H'.

Suppose current hour is 10 so i want to delete lines containing first two less than 10 i.e

02|123
04|321
08|453
09|364
01|789
3
  • You want this in a script? – tachomi Mar 10 '16 at 18:20
  • yes. need logic how to do this simply. – Pacifist Mar 10 '16 at 18:21
  • you can modify the answer for THIS QUESTION to suit your needs. Just use awk -F "|" to specify your field delimiter and compare the $1 to the current hour as in that answer – MelBurslan Mar 10 '16 at 18:26
6
#!/bin/bash
HOUR=`date +%H`
awk -v hour=$HOUR -F"|" '{if($1 >= hour)print}' file.txt > newfile.txt
mv newfile.txt file.txt
1
  • 2
    You could simplify that to awk -v hour=10 -F"|" '$1 >= hour' ... (since print is awk's default action) – steeldriver Mar 10 '16 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.