New User Looking for help with a shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New User Looking for help with a shell script.
# 1  
Old 02-13-2008
New User Looking for help with a shell script.

Greetings all.

I have come across a problem that I am struggling to solve (correctly).

I am looking to write a script that does the following.

1. I have a file that contains the follwing.

file1
#REPLACE_1

some written text...

#REPLACE_2

some more text


2. I then have 2 file that contain lines that should replace the tags.

File REPLACE_1 contains
LINE1

LINE2

File REPLACE_2 contans
LINE3

LINE4



My problem is that I cannot get the whitespaces/newlines to be included in the merged file. Any ideas would be most appreciated as it is starting to drive me round the bend!! Smilie

So to sum up the resulting file should look like this:

RESULTS FILE
LINE1

LINE2

some written text...

LINE3

LINE4

some more text


I thank you all in advance.

Si Smilie


Forgot to say. That at present there are never any whitespaces/newlines. So the followng code I wrote work fine for what I need.

Go easy on it as I am no expert. Current solution is:

grep -i ^#REPLACE_ $file > ./tmpreplace

while read replace
do

awk ' BEGIN { RS="" }
FILENAME==ARGV[1] { r=$0; s=FILENAME }
FILENAME==ARGV[2] { sub(s,r) ; print }
' $replace $file > $tfile

file=$tfile
cnt=$cnt+1
tfile="TMP"$cnt

done < ./tmpreplace

Last edited by WhitesinHolland; 02-13-2008 at 05:27 AM.. Reason: forgot to add present solution.
# 2  
Old 02-13-2008
Code:
awk '/^#REPLACE/ { 
  x = $0
  while ((getline < ("REPLACE_"(x ~ /1/?"1":"2"))) > 0 )
    print
    next
}1' file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 02-13-2008
Many thanks radoulov

That is just the ticket. *Tip of the hat*

Now I just have to try and understand it!! Smilie

The following line:

while ((getline < ("REPLACE_"(x ~ /1/?"1":"2"))) > 0 )

Is perfect for the example I gave. However the only static information is #REPLACE_. This could be for example #REPLACE_DEFS #REPLACE_WORK etc...

So they are not confined to numbers. I guess that what the 1 and 2 is on the above statement (eg the example I gave.)

Is it possible to make this fluid? Smilie I guess by maybe doing another loop looking for the full replace statement?

I will play around and give it a go.

But again thank you very much for putting me in the right direction.

SmilieSmilie

PS. However the filename are ALWAYS the same as the REPLACE tags.

Last edited by WhitesinHolland; 02-13-2008 at 07:10 AM..
# 4  
Old 02-13-2008
I knew it, but I didn't resist Smilie
OK, try this:

Code:
awk '/^#REPLACE/ {
  f = $2
  while ((getline < f) > 0)
    print
    next
}1' FS="#" file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 02-13-2008
You are indeed an addict!!! Smilie

Thank you so so much. Smilie
# 6  
Old 02-13-2008
Fyi:

Quote:
Originally Posted by WhitesinHolland
Forgot to say. That at present there are never any whitespaces/newlines. So the followng code I wrote work fine for what I need.

Go easy on it as I am no expert. Current solution is:

grep -i ^#REPLACE_ $file > ./tmpreplace

while read replace
do

awk ' BEGIN { RS="" }
FILENAME==ARGV[1] { r=$0; s=FILENAME }
FILENAME==ARGV[2] { sub(s,r) ; print }
' $replace $file > $tfile

file=$tfile
cnt=$cnt+1
tfile="TMP"$cnt

done < ./tmpreplace
It generally doesn't work because the second program ("sub(s,r); print") is not executed until all the lines of the first file are consumed. The problem with whitespace has to do with your shell-read command, which ignores whitespace.
# 7  
Old 02-13-2008
Ok. About to screamSmilie

Tried not to come back and look stupid, but the fact of the matter is "I AM!"

So the script radoulov gave is perfect.

Except that it does not more than once if the tag appear xtimes in file1 I only see it once then then same tags get replaced with a whitespace??Smilie

anyone, as I will be honest and say I dont fully understand what radoulov wrote in the first placeSmilie

Thanks in advance Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Switch from Local user to root user from a shell script?

Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su command will do that work but it prompt for the password. and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

How to Login as another user through Shell script from current user[Not Root]

Hi Every body, I would need a shell script program to login as different user and perform some copy commands in the script. example: Supppose ora_toms is the active user ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands ... (9 Replies)
Discussion started by: ujjwal27
9 Replies

4. Shell Programming and Scripting

Shell Script for User AUTH Help

#!/usr/bin/ksh /bin/clear LIST_USERS="user1|user2|user" echo "Please enter the PASSWORD:" stty -echo read PASSWORD stty echo if ; then echo "You have access!" else echo "ACCESS DENIED!" exit fi This above script is not working when I auth more then one user w/in... (22 Replies)
Discussion started by: shiv2001in
22 Replies

5. Shell Programming and Scripting

Shell Script to change a user password using script

Hi Experts, I had tried to executes this script to change the user password through script: No lines in buffer #!/bin/ksh cat /etc/passwd | grep -v userid >> /tmp/pass.tmp1 cat /etc/passwd | grep userid >> /tmp/pass.tmp2 PASS1=`cat /tmp/pass.tmp2 | cut -d ":" -f2` PASS2=`q2w3e4r5` sed... (3 Replies)
Discussion started by: indrajit_renu
3 Replies

6. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

7. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

9. Shell Programming and Scripting

su to another user in shell script

Hi All, I want some help in creating a shell script. I am running a script thru user1, then I need to su to user2, run a script from that user and return back to user1 and proceed further. Please help me. Abhi (1 Reply)
Discussion started by: akdwivedi
1 Replies

10. UNIX for Dummies Questions & Answers

User Shell script

I'm very new at this -- HELP! I need to know how to terminate a users session when they exit a shell script menu? Any ideas would be much appreciated! Holli (2 Replies)
Discussion started by: HOlli
2 Replies
Login or Register to Ask a Question