Shell scripting -append a new string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting -append a new string
# 1  
Old 09-23-2010
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.
# 2  
Old 09-23-2010
Hi, welcome to the forum. Could you show us what you tried so far and where you are stuck?
# 3  
Old 09-24-2010
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)

Code:
#!/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

Last edited by Scott; 09-25-2010 at 05:08 AM.. Reason: Please use code tags and indent code
# 4  
Old 09-24-2010
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:

shell code:
  1. #!/bin/sh
  2. # this program will search for the servername in a directory and adds a new cluster servername
  3. i=0
  4. found=false
  5. echo "Enter the filename:"
  6. read FILENAME
  7. echo "enter the server name:"
  8. read SERVERNAME
  9. a=$SERVERNAME
  10. for f in $FILENAME
  11. do
  12.   if grep csslu$SERVERNAME $f > /dev/null ;then
  13.     found=true
  14.     echo "file \"$f\" contains server csslu$SERVERNAME"
  15.     rem=$(( $SERVERNAME%2 ))
  16.     if [ $rem -eq 0 ]
  17.     then
  18.       i=$(( a+1 ))
  19.     else
  20.       i=$(( a-1 ))
  21.     fi
  22.     if grep csslu$i $f > /dev/null ; then
  23.       echo "the server name csslu$i already exists in file \"$f\", nothing to add"
  24.     else
  25.       echo "csslu$i" >>$f
  26.       echo "printing tail of $f to check if the cluster is addedd..."
  27.       tail -3 $f
  28.     fi
  29.   fi
  30. done
  31. if [ $found = false ]; then
  32.   echo "csslu$SERVERNAME not found"
  33. fi

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

S.

Last edited by Scrutinizer; 09-24-2010 at 09:38 PM..
# 5  
Old 09-26-2010
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.
# 6  
Old 09-26-2010
Just remove csslu everywhere in the script.
# 7  
Old 09-27-2010
thank you mate . much appreciated. good luck
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies

2. Shell Programming and Scripting

How can I extract digits at the end of a string in UNIX shell scripting?

How can I extract digits at the end of a string in UNIX shell scripting or perl? cat file.txt abc_d123_4567.txt A246_B789.txt B123cc099.txt a123_B234-012.txt a13.txt What can I do here? Many thanks. cat file.txt | sed "s/.txt$//" | ........ 4567 789 099 012 13 (11 Replies)
Discussion started by: mingch
11 Replies

3. UNIX for Beginners Questions & Answers

Shell Scripting , need to search and replace a string in AIX

Hi Guys, I need to search and replace a string in AIX using variables and should be case insensitive. I am able to search and replace using below command but its not working as case insensitive. cat abc.txt | sed -e 's/$a/$b/g' > abc.txt But i need to perform this with case... (9 Replies)
Discussion started by: mohit_vardhani
9 Replies

4. Shell Programming and Scripting

Shell scripting: frequency of specific word in a string and statistics

Hello friends, I need a BIG help from UNIX collective intelligence: I have a CSV file like this: VALUE,TIMESTAMP,TEXT 1,Sun May 05 16:13:05 +0000 2013,"RT @gracecheree: Praying God sends me a really great man one day. Gotta trust in his timing. 0,Sun May 05 16:13:05 +0000 2013,@sendi__... (19 Replies)
Discussion started by: kraterions
19 Replies

5. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

6. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

7. Shell Programming and Scripting

Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of... (2 Replies)
Discussion started by: chagan02
2 Replies

8. Shell Programming and Scripting

Shell scripting string manipulation

Hi, if I have a string delimited by commas how can I put each character on a new line followed by a carriage return, and output this to a filee.g from: s,t,r,i,n,g to s t r i n g thanks you (3 Replies)
Discussion started by: Wahmed9
3 Replies

9. Shell Programming and Scripting

Truncate and Append in unix shell scripting

Hi All, I have a long log file (abc.log) which contains large volume of data that need to be inserted into the database as clob. I am having problem in inserting the long log file into the clob field if the cahracters in the file exceeds. So iwould like to truncate the file upto 32767... (3 Replies)
Discussion started by: rajeshorpu
3 Replies

10. Shell Programming and Scripting

String comparision in shell scripting

Hi Guys, I am new to scripting I have written a code to compare strings,but I am getting some Exception Code snippet: MODE="D" if ]; then . $file1 fi Error: ./BatchJobs.sh: [[: execute permission denied I have given all Execute permissions to the script(chmod 755... (2 Replies)
Discussion started by: Anji
2 Replies
Login or Register to Ask a Question