Editing a file using a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing a file using a script
# 15  
Old 06-05-2003
Oh duh! Sorry Smilie

Yes! It worked! I had to edit the path it was searching for as it was changing things I didn't want it to change.

Would it be possible to change the code so that I only enter one directory (I will run the script on an individual directory basis) and also to simply over write the file it is editing?

I have a test system I'm running on so it doesn't matter if I break it. Smilie
# 16  
Old 06-05-2003
If you want to overwrite the file, replace the three references to ${FILE}_new with TMP_00 and add this bit of code to the bottom of the script:
Code:
    else
      echo $LINE >> TMP_00
    fi
  done < $FILE
  mv TMP_00 $FILE
  rm TMP_00
done

Quote:
Originally posted by Ypnos
Would it be possible to change the code so that I only enter one directory (I will run the script on an individual directory basis) ... ?
Not quite sure what you mean by this though.. could you rephrase the question?
# 17  
Old 06-05-2003
From what I have understood in your script, you enter two directories at the top and the script finds an entry which I specify (in this case LOC=/u/) and changes the first entry it finds to dirOne and the second one it finds to dirTwo?

If that's right, I would like to enter just one directory and have the script change entries which contain LOC=/u/[blah]/main to LOC=/u/dirOne/main.

I hope I'm doing a good job explaining... :|
# 18  
Old 06-05-2003
Yes, that can work too.. see if this works:
Code:
echo -n "Enter a directory: "
read DIR
for FILE in `ls`
do
  while read LINE
  do
    echo $LINE | grep -q "/u/.*/main/"
    if [ $? -eq 0 ]
    then
      echo "/u/$DIR/main/" >> TMP_00
    else
      echo $LINE >> $TMP_00
    fi
  done < $FILE
  mv TMP_00 $FILE
  rm TMP_00
done

This could be done really quickly with a sed command:
Code:
echo -n "Enter a directory: "
read DIR
for FILE in `ls`
do
  sed -e 's/\/u\/.*\/main\//\/u\/'$DIR'\/main\//g' $FILE
done

but unfortunately to enter a directory like this/ismy/directory you'd have to type it in as this\\\/ismy\\\/directory

Last edited by oombera; 06-05-2003 at 02:32 PM..
# 19  
Old 06-05-2003
Sorry if this is mentioned above, I was trying to follow the whole "conversation"...

Does the script change between the times you deploy from one location to another?

You could use a "skeleton" script and only edit the variables.

Put a key word in the script so that when you deploy it, you only have to edit that key word. Don't change the already deployed script... only edit the skeleton and save it where you want it to be next... (new directory)...

ie I have the following control card detail.src that my users update via script.
R,detail.lis,0,<CO>,?,<DATERNG>,?,?,?,?,?,<ST>,1,0,60

the script prompts the user for a date rangs (from and to) and then a company code and state code then uses the skeleton file as input to create a new file.... :

sed -e "s/<DATERNG>/$DATE1,$DATE2/" \
-e "s/<CO>/$COMPANY/" \
-e "s/<ST>/$STATE/" <detail.src >detail.dat
Everytime it saves out a new file.

Does this help?
# 20  
Old 06-05-2003
I'm not positive if this is what you're asking, but instead of using a skelaton file (I would call it a template) with my script I'm making the assumption that the directory already has some old configuration files in it, which my script essentially is using as templates to create new configuration files...
# 21  
Old 06-06-2003
Thanks for your input MizzGail, but I'm afraid oombera is right... I'm not sure which script you refer to when you ask if it changes when it changes from one location to another... The idea is that the script stays the same and the only input the user should have, are the new pathnames in the config (.ini) files, which are entered when oombera's script asks for them. Smilie

Oombera, I tried your latest script and seems to have wiped the ini files and now they only contain LOC=/u/[DIR]/main... hehehe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

2. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi Scott, My previous post was not for any school or college projects. I am currently working with a IT company (Cannot provide more details than this). I am trying to implement the below script in my day-to-day work, Apologies for the confusion in previous post :). My question remains same... (4 Replies)
Discussion started by: Nishant Ladiwal
4 Replies

3. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi All, I have below requirements for my project: 1. Building a shell script which connects to a remote server 2. running script on local machine as user 'A' 3. connecting to server using user 'B' with password 4. login with a powerbroker role 'P' (asks for same password as 'B') on that... (1 Reply)
Discussion started by: Nishant Ladiwal
1 Replies

4. Shell Programming and Scripting

editing single line in html file in perl script

Hi Folks, It is regarding the perl scripting. I have an html file(many files) which contains the below line in the body tag. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> Now I want to read that html file through perl... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

Problems editing file with awk in bash script

Hello dear users, here I have a script to manipulate .csv files that are like this originally: And I need to make a script to delete certain fields. Each field is separated with a comma. So, here is my script (at least a part of it): Field $1 is composed of a name, and then a... (5 Replies)
Discussion started by: sr00t
5 Replies

6. Shell Programming and Scripting

editing a file in a script

Gurus, I need to write a shell script that will calculate hash value of a file, opens the file in an application for example vi editor. The application can read or modify the contents of the file. When application exists second part of my script will kick in and recalculate the hash value. File... (1 Reply)
Discussion started by: c0kazaz
1 Replies

7. Shell Programming and Scripting

editing a file using shell script

HI all, I have file in the below format 1111111111_222222222_3333333 111111_22222_33333 11111111_222222_33333333333 i need to display this file like this 2222222_1111111111 22222_11111111 22222222222_1111111111111 can anyone help me with this Thanks in advance (1 Reply)
Discussion started by: saravanan71184
1 Replies

8. Shell Programming and Scripting

editing excel file through shell script

Hi, I am having a business file in excel having charts based on data already present on it. I would like to add new rows after the existing data and refesh the chart on it using shell script. For example-- In excel file in "sheet1", There is some data in first 10 rows ( from column A to F).... (0 Replies)
Discussion started by: sanjay1979
0 Replies

9. Shell Programming and Scripting

editing a file via a shell script ??

Morning All: I know this might be easy but since I don't do this very often I get stumped real quick... Sun box Solaris 8 ksh... I need to edit a file via a shell script. In this file I need to locate one specific line and then remove that line plus the next 20 line below that.... Any... (2 Replies)
Discussion started by: jimmyc
2 Replies

10. UNIX for Dummies Questions & Answers

Editing a file in a shell script

Using Solaris 8. I need to create a shell script that will edit a text file. I need to look in the text file and do a search and replace. For instance, the text file name is always 'filename'. I need to open filename and replace every instance of 'oldtext' with 'newtext'. 'oldtext' is static. ... (3 Replies)
Discussion started by: jowpup
3 Replies
Login or Register to Ask a Question