Search code examples
bashfileunixawkfilefilter

Filtering a file


I have a file that contains many lines :

at 12:00 the schedule is :

first_task:eating:fruit
second_task:rest:onehour
third_task:watching:horrorfilm  

at 18:00 the schedule is :

first_task:eating:vegetals
second_task:rest:threehours
third_task:watching:manga 

at 22:00 the schedule is :

first_task:eating:nothing
second_task:rest:sevenhours
third_task:watching:nothing

I tried to search before Asking . but I didn't find a way to filter the file the way I liked .

I would like to get a filtered file like this : for example If I would like to know what I am going to eat today .

at 12:00  eating:fruit
at 18:00 eating:vegetals
at 22:00 eating:nothing

Is there any way to do this using awk or bash ?


Solution

  • Using awk, you can do it as:

    awk -F '[\ :]' '/the schedule is/{h=$2;m=$3} /eating/{print "at "h":"m" eating:"$3}' filename
    

    Output:

    at 12:00 eating:fruit
    at 18:00 eating:vegetals
    at 22:00 eating:nothing
    

    This will give correct answer, even if you have eating as second or third task.