Sponsored Content
Top Forums Shell Programming and Scripting Shell scripting -append a new string Post 302456654 by Scrutinizer on Friday 24th of September 2010 08:33:29 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy