Filtering a file

76 views Asked by At

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 ?

3

There are 3 answers

5
Rakholiya Jenish On BEST ANSWER

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.

2
jmdeldin On

You can do this pretty simply with grep and sed. Assuming your example file is saved as f.txt:

egrep '^at|eating' f.txt | sed 's/first_task://' |
  sed  's/the schedule is ://' | paste -d' ' - - | column  -t

returns

at  12:00   eating:fruit
at  18:h00  eating:vegetals
at  22:h00  eating:nothing
0
Vijay On
perl -lne '$a=$1 if(/(^at\s+[^\s]*?)\s/);print $a." ".$1 if(/(eating:.*)$/)'

Check here for output.