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.
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
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`}}'
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"
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.
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 ----------