script to search and edit scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to search and edit scripts
# 1  
Old 01-07-2008
script to search and edit scripts

Hi all,
can you please help me in this one..

i have a many scripts in a directory & i get many requests to change the code of a particular script.

for example file abc.txt contains
#!/bin/bash
mumbai 102403445
chennai 123980123
delhi 3456268468
kolkata 465376832
#kolkat 462945959

i get a request to change the value ( population of kolkata ) 46852342424

my script should to

#!/bin/bash
mumbai 102403445
chennai 123980123
delhi 3456268468
#kolkata 465376832 # i have to keep old entry as comment
kolkata 4685234242
#kolkata 462945959

so my script should search for KOLKATA and keep old entry as comment and change it to new value. please help me..
# 2  
Old 01-07-2008
script to mark old record & create new

>fixabc kolkata 468523424

SCRIPT fixabc
findrec=$1
newval=$2
old=$(cat abc.txt | grep "^"$findrec)

grep -v "^"$findrec abc.txt >abc2.txt
echo "#"$old >>abc2.txt
echo $findrec" "$newval >>abc2.txt


>>>NOTES
first couple lines takes input
reads/finds existing line
uses grep to copy everything except
writes the old line begin with #
writes the new line
AND the "^" is an anchor to read from the first column/character in a line
# 3  
Old 01-07-2008
Java

I would recommend you don't put #!/bin/bash at the start of a text file like that unless you are ok with calling those placenames as commands.

If you want it to leave the order of the file intact, try this:
Code:
#!/bin/sh
while read line
do
  if echo $line | egrep "^$1" > /dev/null
  then
    # We have found our line
    echo "#$line"
    echo "$1 $2"
  else
    echo $line
  fi
done

usage: scriptname.sh kolkata 46852342424 < inputfile.txt > outputfile.txt
# 4  
Old 01-08-2008
thanks a lot, your code works perfectly!!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies

2. Shell Programming and Scripting

Grep from FileA, search in FileB, edit FileC > Output

Hello, Similar question to my previous posts. I am sorry for the trouble... Just checked my old threads but I can not implement any solution into this case.. My aim is to grab each line in fileA, check it in fileB and merge with fileC (tab separated) in corresponding line as given below: FileA:... (2 Replies)
Discussion started by: baris35
2 Replies

3. UNIX for Advanced & Expert Users

File edit script exceptions how to pass to script

i wrote script for if file exists script do file edit problem with the script get stuck not existing as for exit i mentioned exit 0 , and how to give the exception for script it should add ./script -- add hi ./script --add "hi how are you" now below script with case it is working for... (0 Replies)
Discussion started by: markjohn1
0 Replies

4. UNIX for Advanced & Expert Users

Search in Scripts

Hi I'd like to search content of my apple scripts and list the results. What I have is: find /Scripts -name "*.scpt" -exec osadecompile '{}' \; | xargs -0 grep -l "POSIX" With that instruction I get no result. What's wrong? Any tip or hint is welcome. Regards Lazy (4 Replies)
Discussion started by: lazybaer
4 Replies

5. OS X (Apple)

Need Help with GREP REGEX scripts for common BB-EDIT text-editing

Hi Everybody.. I'm a "newbie" to using Command-line... A few half-remembered DOS commands from 30 years ago, and the very handy "Sudo rm -R pathname" REMOVE command... I do a lot of "cleaning" of plain-text OCR text files. with assorted common line-break, punctuation and capitalization... (1 Reply)
Discussion started by: TheMacGuy
1 Replies

6. Debian

Problems with Crontab not executing scripts after edit

Hi all I installed Debian and i have a few scripts that outputs what is happening. The wierd part...after fresh install all works ok but after i open or edit Crontab it stops executing the scripts...and scripts runs manually so its not a problem with scripts...what happens is that i usually... (3 Replies)
Discussion started by: ro0t3d
3 Replies

7. Shell Programming and Scripting

search and edit in the same file using awk

Hi, I am having a user.txt contains the name of users and passwd.txt file contains as passwd.txt $cat usr.txt root bin daemon cap $cat passwd.txt root:x:0:0:root:/root:/usr/bin/ksh bin:x:1:1:bin:/bin:/sbin/csh daemon:x:2:2:daemon:/sbin:/usr/bin/ksh adm:x:3:4:adm:/var/adm:/sbin/nologin... (4 Replies)
Discussion started by: Manabhanjan
4 Replies

8. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

9. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

10. Shell Programming and Scripting

How search,edit and save the file

Hi All, I want to edit a file using shell script..For ex...a file called /etc/passwd..here I am searching for "ftp" if it is there just change it to "tftp" without using any temporary file. (3 Replies)
Discussion started by: Vichu
3 Replies
Login or Register to Ask a Question