Shell scripting -append a new string

hi all ,

i am looking for a shell script which looks into a directory of text files and searches for a string(abc123) in all the text files and if that exists add a new line(abc124) in all the *.txt files automatically, or if (abc124) exists add (abc123) can you all please help me.

Hi, welcome to the forum. Could you show us what you tried so far and where you are stuck?

thank you Scrutinizer. i just started learning the shell scripting.
so far i have written a script to find a servername from a text file and add a new server name (ex it csslu258 exists, add csslu259 or if csslu259 exists add csslu258)

#!/bin/sh
# this program will search for the servername in a directory and adds a new cluster servername 
let i=0
echo "Enter the filename:"
read FILENAME
echo "enter the server name:"
read SERVERNAME
a=$SERVERNAME
if grep $SERVERNAME $FILENAME > /dev/null ;then 
  echo "file contains server csslu$SERVERNAME"
  rem=$(( $SERVERNAME%2 ))
  if [ $rem -eq 0 ]
  then
    let i=a+1
  else
    let i=a-1
  fi
  if grep $i $FILENAME > /dev/null ; then
    echo "the sever name csslu$i already exists nothin to add"
  else
    echo "csslu$i" >>$FILENAME
  fi
  echo "printing tail of $FILENAME to check if the cluster is addedd..."
  tail -3 $FILENAME
else
  echo "csslu$SERVERNAME not found"
fi

my inputs are
filename -sample.txt
servername-258
. i need to do the same for all the files in the directory.
if i input file name as *.txt, the scripts creats a new file *.txt and add the sever name

hope you could understand the requirement.

thank you

Hi,

If you want to be able to input *.txt instead of a file name or multiple filenames, you need to put a mechanism in your script that loops through all the file names. Here is an example of an extra for loop (line 10) that goes through all the input file names. If you input a single file name then it will only go through this loop once:

[highlight=shell]
#!/bin/sh
# this program will search for the servername in a directory and adds a new cluster servername
i=0
found=false
echo "Enter the filename:"
read FILENAME
echo "enter the server name:"
read SERVERNAME
a=$SERVERNAME
for f in $FILENAME
do
if grep csslu$SERVERNAME $f > /dev/null ;then
found=true
echo "file \"$f\" contains server csslu$SERVERNAME"
rem=$(( $SERVERNAME%2 ))
if [ $rem -eq 0 ]
then
i=$(( a+1 ))
else
i=$(( a-1 ))
fi
if grep csslu$i $f > /dev/null ; then
echo "the server name csslu$i already exists in file \"$f\", nothing to add"
else
echo "csslu$i" >>$f
echo "printing tail of $f to check if the cluster is addedd..."
tail -3 $f
fi
fi
done
if [ $found = false ]; then
echo "csslu$SERVERNAME not found"
fi

[/highlight]

I replaced the let ... through the $(( )) construct which is the posix way.

S.

thank you so much mate,
but now my input should be like server name csslu245 not just the number, and i want to add the new cluster to the new line.
example the text files contain

csslu245
csslu258
csslu260
cssln123
.

i want to search for either csslu/cssln and add the respective.
how do i modify from here.

Just remove csslu everywhere in the script.

thank you mate . much appreciated. good luck