Insert data based on pattern

if it is finding some data based on pattern 'test' then insert else if has no data based on the pattern 'test' then exit successfully

  
cat file | grep test > file2

Any attempts (which I can't see in your above) from your side? Did you consider searching these forums for this very common question?

I tried below script but getting error .'else' is not expected'

 
#!/bin/ksh
grep test file
if [ $? -eq 0 ]
   `cat file | grep test > file2`
else
   echo " Exit sucessfully"
fi

That doesn't insert anything. I don't get the feeling you were searching too intensively for a solution. And, your specification is less than sparse. Howsoever, try - given you want to insert file2 -

sed '/test/r file2' file
1 Like