Finding a specific word

Hi,

I am trying to develop a script which should find a word if a particular word exists.

Below is the content of the file.

insert_job: test_job ----> job name
days_of_week: all
start_times: "16:00"
date_conditions: 1
 
insert_job: test_job2 ----> job name
days_of_week: all
start_times: "16:00"

Now my objective is to find the job name which doesn't have date_conditions even though they have both start_times and days_of_week.

In the above sample it should display test_job2 as output as it doesn't contain days_of_week and start_times.

Thanks for your help in advance.

Regards,
rpatty

 
#!/bin/ksh
awk '{ if($1=="insert") printf("\n%s",$0); else printf(" %s",$0); }' input_file > temp
grep -v "date_conditions" temp | awk '{ print $2 }'
1 Like

Try:

awk '!/date_conditions/{print $2}' RS= infile
3 Likes

thanks for your reply on my query.
The code which you have sent is not working. As I mentioned in my query. The script needs to check whether job name(its under insert_job) contains days_of_week and start_times parameter then only it should go for checking date_conditions parameter.
If there are no days_of_week and start_times parameter for a particular job name it should skip that job.
If job name contains both days_of_week and start_times parameter and if it doesn't contain date_conditions parameter it should print job name else not.

At the end of the script it should print only job name thats under insert_job parameter.

Thanks once again for replying to my query.

awk '/days_of_week/ && /start_times/ && !/date_conditions/{print $2}' RS= infile
1 Like

Will this work for you:

1 Like

hi Panyam,

Thanks for your reply.

Its giving me systax error after /test/. what kind of action does /test/ peforms. I am getting similar error after /insert/ and /date/ also.

Please help me on this.

Thanks,
rpatty

can you try with:

i am working on Linux operationg system

 
[ravi@testbox ~]$ uname -a
Linux testbox 2.6.20-1.2948.fc6 #1 SMP Fri Apr 27 19:18:54 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
 
[ravi@testbox ~]$ cat input_file
insert_job: test_job ----> job name
days_of_week: all
start_times: "16:00"
date_conditions: 1
insert_job: test_job2 ----> job name
days_of_week: all
start_times: "16:00"
 
insert_job: test_job3 ----> job name
days_of_week: all
start_times: "16:00"
date_conditions: 1
 
insert_job: test_job4 ----> job name
days_of_week: all
start_times: "16:00"
 
[ravi@testbox ~]$  awk '$0 ~ /^$/ && job ~ /test/ {printf("%s\n",job);job="";next } /insert/ {job=$2;next} /date/ {job="";next}'  input_file
test_job2
test_job4

 

Hence , it works in linux too!

Thanks alot Ravi. It working. I really appreciate your help.

Take care and have a great weekend..:slight_smile:

I am facing a new problem. I want to remove line which is having blank space above and below to it.

Below is the sample file.

insert_job: fsetlbqa_fsw_ibsfile_box job_type: b
date_conditions: 1
start_times: "09:00"
 
insert_job: fsetlbqafsw028ibsetlfile job_type: c
 
insert_job: fsetlbqa_fsw_ETLB_spc_rep_box job_type: b
date_conditions: 1
days_of_week: mo,tu,we,th,fr,sa,su
start_times: "06:00"
 
insert_job: fsetlbqafswW999spacereport job_type: c

My output should be

insert_job: fsetlbqa_fsw_ibsfile_box job_type: b
date_conditions: 1
start_times: "09:00"
 
insert_job: fsetlbqa_fsw_ETLB_spc_rep_box job_type: b
date_conditions: 1
days_of_week: mo,tu,we,th,fr,sa,su
start_times: "06:00"

I want to remove the lines which are blank spaces above and below to the line. I am trying to get it done by using sed. But i am not getting correct solution.

Love Scrutinizer solution, anyway if you want to "give a try" to perl you will notice it's Extensibility:

#!/usr/bin/env perl

open(DAT, shift) || die("Could not open file!");
@data=<DAT>;
close(DAT);
foreach ( @data ) {
   chomp;
   if ( /([^:]+):\s*(.*)/ ) {
      $key=$1;
      $value=$2;
      $name=$value if ( $key =~ /insert_job/ && $value ne $name ) ;
      $job{$name}{$key}=$value;
      }
   }

foreach $name ( sort keys %job ) {
      if  ( ! $job{$name}{date_conditions} ) {
         print $name."\n";
         }
   }

Usage:

script file

something like this on the i/p file to remove the "insert" lines preceeded and followed by blank lines.

 
awk '/^$/ {p=1;q="";next} /insert/ {q=$0;next} q { print q;q=""} 1' input_file

Thanks alot panyam..Its working fine..

---------- Post updated at 07:12 AM ---------- Previous update was at 06:58 AM ----------

Hey Panyam,

Could you please explain me the above code. It will be very helpful for me.

Thanks,

awk '/^$/ {p=1;q="";next} /insert/ {q=$0;next} q { print q;q=""} 1' input_file

  1. /^$/ --> Empty line go to next

  2. /insert/ --> have insert in the line , store the line in "q" and go to next line

  3. if "q" is not empty print it and make "q" empty.

for your input ,

 
 
insert_job: fsetlbqafswW999spacereport job_type: c


First line is blank , so step (1) is matched , hence will go to next line
For second line ,step(2) is matched , since the line have "insert" in to. so store it in "q' and go to next line
For third line , agian it's blank , so step(1) will be matched and will make "q" empty and go to next line.

Regards
Ravi

Thanks alot Ravi. That clears my doubt.