One-liner to insert text if !found

brendat January 30, 2009 at 13:40:28
Specs: Linux i586
I'm looking for a good one-liner, perl, sed, whatever works, to search a specific text file for a line of text, if it doesn't find it, then insert the text at the end of the file.

Thanks!


See More: One-liner to insert text if !found

Report •


#1
February 2, 2009 at 04:26:36
::just copy everything here and paste into a bat file (ie: 1.bat) - and read it.


set string=YOUR LINE STRING HERE
set myfile=YOUR FILE HERE WITH FILE EXTENSION
set lastline="LINE YOU WANT ADDED AT END OF TEXT FILE IF STRING IS NOT FOUND"

::+==============+ DON'T EDIT BELOW HERE +==============+
::+==============+ DON'T EDIT BELOW HERE +==============+
::+==============+ DON'T EDIT BELOW HERE +==============+
@echo off & cls
find /i "%string%" < %myfile% > temptodelete.txt %1
if errorlevel 1 goto :error
echo Your line string was found!!!
pause>nul
del temptodelete.txt
goto :eof
:error
echo.>> %myfile%
echo %lastline%>> %myfile%
echo Your line string was not found. A line has been added to the end of your file.
pause>nul
del temptodelete.txt


Report •

#2
February 2, 2009 at 06:29:38
perl -pi.bak -e 'BEGIN{$str = "some string";}$seen++ if /$str/; END{print $str if !$seen}' file.txt
==========================================================================

Report •

Related Solutions


Ask Question