String search and generating file

Hi Gurus,

I have a requirement as below

I have text file a.txt which contains

hi hello 
process update
status
Output for file Ok to Proceed no issues good
data arrangement

My requirement here is i need to read the file and check for the words
"OK to Proceed" and if it is available need to generate an indicator file called
"Go.txt"

IF else no file should not be generated.

Here one more thing to be considered is, file also contains "Not Ok to Proceed" sometimes if the process is not gone thru well.

Basically i need to generate an indicator file, if the process is OK by searching "Ok to Proceed" string or else not generating the file if it contains "Not ok to Proceed"

I am trying following code but not succesfull upto now

#!/bin/ksh
DFT_VALUE='OK to Proceed'
ACTUAL_VALUE=`egrep 'OK to Proceed' respectivefolder/a.txt`
if [$DFT_VALUE = $ACTUAL_VALUE] then
  touch a.txt
else
  exit
fi

Here my problem is how to get only the string from the file to comapre with the variable.

Any suggestions or code will be appreciated

Thanks in advance,
San

Thanks in Advance
Sandeep

You can search directly for your variables content by using the return-value of grep: if the string is found, grep returns 0, otherwise 1 (respectively non-null, according to the POSIX standard).

Therefore your code could be shortened to this:

#!/bin/ksh
DFT_VALUE='OK to Proceed'

if [ $(grep -q "${DFT_VALUE}" /path/to/file ; print - $?) -eq 0 ] then
     touch /path/to/a.txt
fi

exit 0

Still, there is a little problem: the string "Not OK" will be matched by the expression "OK" too. You will need a second grep to separate the "Not OK"s from the "OK"s.

#!/bin/ksh
DFT_VALUE='OK to Proceed'

if [ $(grep -q "Not ${DFT_VALUE}" /path/to/file ; print - $?) -gt 0 ] ; then
     if [ $(grep -q "${DFT_VALUE}" /path/to/file ; print - $?) -eq 0 ] ; then
          touch /path/to/a.txt
     fi
fi
exit 0

I hope this helps.

bakunin

cat abc.txt

hi hello
process update
status
Output for file Ok to Proceed no issues good
data arrangement

cat xyz.txt

Output for file Not Ok to Proceed no issues good

This is assuming the file has only 1 instance either OK or not OK . If that is not the case add a flag and in the end touch the file depending on the val

cat abc.txt | perl -e 'while(<>){ chomp; my $line = $_ ;if ($line =~ m/.*Ok\s*to\s*Proceed/ && $line !~ m/Not Ok to Proceed/){ print "$line\n" ; `touch Go.txt`}}'

cat xyz.txt | perl -e 'while(<>){ chomp; my $line = $_ ;if ($line =~ m/.*Ok\s*to\s*Proceed/ && $line !~ m/Not Ok to Proceed/){ print "$line\n" ; `touch Go.txt`}}'

Cheers,

grep -E '^(.*[^N][^o][^t] )?Ok to Proceed' a.txt && touch Go.txt

use following code:-

sed -n '/Ok to Proceed/ p' input_file | nawk '($0 !~ "Not") { "touch GO.txt" | getline }'

BR

Hi

Thanks for the response.Sorry for late reply.

When i am executing that command its saying "illegal command grep with q "

when i replaced "q" with "s" missing operator

This is my o/p when i used the code you mentioned.

may be we have the old version of the UNIX

What about using fgrep or -F flag?
Maybe something like this... If the fixed string "Not ok to proceed" is not found in the file, then just check if "ok to Proceed" exists; if it does, then generate the output.

if [[ `grep -Fi "Not ok to Proceed" ./a.txt` -ne 0 ]]
   then  
   if [[ `grep -ic "ok to Proceed"` ]]
   then
        touch go.txt
   fi  
fi
   

Try with fgrep instead of grep -F if it doesn't work properly.

Thanks for all of ur responses
Its working with grep.

I have one more question on this, suppose if i want to go and check the text file based on line no For example, I need to go to the line 15 in text file and then check for the String "OK to Proceed" or "Not ok to Proceed" because
my text file will contain a comment on 10th line with "Not Ok to Proceed"

So either i need to go based on line number or need to check for the second occurence of the "Not Ok to Proceed"

Please advice

Thanks in Advance,
Sandeep:)

You can do various things, such as:

  1. Strip the comment, and pipe only appropriate input to grep as
sed 's/^#//' | grep '<PATTERN>'
  1. Extract part of file, using tail or head as:
    tail -n +11 FILE | grep '<PATTERN>'

  2. or search for 2nd occurrence and so on.

Select an appropriate way, by analyzing your data.

Not clear :rolleyes:

awk '!/Not/ && /Ok to Proceed/ && NR==15{print "" > "Go.txt"}' file
#!/bin/bash  -v
grep "OK to proceed" check.txt
if [ $? -eq 0 ]
then
 touch "Ok.txt"
fi

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

this can be written as,

grep "OK to proceed" check.txt 1>/dev/null && touch "Ok.txt"

You can change the path to be

sed -n '/Ok to Proceed/ p' input_file | nawk '($0 !~ "Not") { "touch /the/path/of/file/GO.txt" | getline }'

BR

---------- Post updated at 08:02 AM ---------- Previous update was at 07:59 AM ----------

to change the path use the above bold phrase

awk '!/Not/ && /Ok to Proceed/ && NR==15{print "" > "Go.txt"}' file

the above code means:-
if the row does not contain "NOT" && contain "Ok to Proceed" && the row number ==15 then do
print "nothing" and send it to "GO.txt" file

---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------

last sentence means create GO.txt

---------- Post updated at 07:36 AM ---------- Previous update was at 07:36 AM ----------

and the file path is the current path.