adding text with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding text with sed
# 1  
Old 04-02-2012
adding text with sed

Code:
sed 's/<\/body>/<A HREF='"$index"'>'"$description"'<\/A>\
<\/body>/' "$index"

This will work from the command prompt, but not from my ksh script.
Why not?

Code:
sed: command garbled: s/<\/body>/<A HREF=/accounts/students/b/bmwg6c/public_html/index.html>I would like to call it white shorts!<\/A>\
<\/body>/

This is my error message.

Last edited by robin_simple; 04-02-2012 at 05:25 PM..
# 2  
Old 04-02-2012
Without seeing the code of your ksh script, I can't possibly say.
# 3  
Old 04-02-2012
ksh code

Code:
#!/bin/ksh
PATH=/usr/bin:/usr/dt/bin:/usr/openwin/bin:/usr/sbin:/usr/ccs/bin:/usr/ucb:/usr/local/bin:.		#BEGIN Find public_html directory.
	pub_html_num=`find ~$USER -type d | grep public_html | wc -l`							
	if [[ $pub_html_num -eq 0 ]]
	then
		echo There are no public_html directories.
	elif [[ $pub_html_num -eq 1 ]]
	then
		pub_html=`find ~$USER -type d | grep public_html`
	else
		set -A pub_html_arr `find ~$USER -type d | grep public_html`
		again=1
		while [[ $again -ne 0 ]]
		do
			echo You have more than one public_html directory for consideration.
			echo Which would you like to use?
			counter=1
			for i in ${pub_html_arr[*]}
			do
				echo "${counter}. $i"
				let counter=$counter+1
			done
			print -n " :"; read choice;
			if [[ $choice -gt 0 && $choice -lt $counter+1 ]]
			then
				pub_html=${pub_html_arr[$choice-1]}
				again=0
			else
				echo You entered $choice, which was wrong.
				again=1
			fi
		done
	fi
	
	set -A file_names_minus_index `find $pub_html -type f | sed 's/^.*index.html//`					#BEGIN Finding unmatched files.	
	set -A file_names_html `find $pub_html -type f | grep "[.]html$"` 

	set -A matched																					#find referenced files.
	for i in ${file_names_html[*]}
	do
		counter_j=0
		for j in ${file_names_minus_index[*]}
		do
			match=`cat $i | grep "<a.*href=\"$j\".*>.*<\/a>" | wc -l`
			if [[ $match -ne 0 ]]
			then
				file_names_minus_index[$counter_j]=0
			fi
			let counter_j=$counter_j+1
		done
	done
														
	set -A unmatched																				#make array of files not referenced.
	counter_unmatched=0
	for k in ${file_names_minus_index[*]}
	do
		if [[ $k != 0 ]]
		then
			unmatched[$counter_unmatched]=$k
			let counter_unmatched=$counter_unmatched+1
		fi
	done																							#END Finding unmatched files.

echo ${#unmatched[*]}
	unmatched_amt=${#unmatched[*]}
	if [[ $unmatched_amt -gt 0 ]]											#BEGIN Placing the link.
	then
		index=`find $pub_html -name index.html`
		for i in ${unmatched[*]}
		do
			echo $i is not referenced in a .html file.  Would you like to reference it?
			print -n "Name of file, Enter or none:"; read reference 
			if [[ -z "$reference"  ]]									#check if user pressed enter.
			then
				if [[ -w "$index" ]]									#check if file is writable.
				then
					echo Writing . . .
				else
					echo "$index" is not writable.  Would you like to make it writable?
					read write
					repeat=1
					while [[ $repeat -ne 1 ]]
					do
						if [[ "$write" = y ]]							#check if user wants file to be writable.
						then
							chmod 700 "$directory"
							repeat=0
						elif [[ "$write" = n ]]							#check if user entered n.
						then
							echo ok.
							repeat=0
						else
							echo Please enter y or n.
							repeat=1	
						fi
					done
				fi
				echo What would you like to call your link?
				read description
sed 's/<\/body>/<A HREF="$index">"$description"<\/A>/' "$index"
			elif [[ $reference = none ]]									#check if user typed none.
			then
				continue
			elif [[ $reference != $i ]]									#check if user typed something other than enter and none.
			then
				if [[ -w "$reference" ]]								#check if file is writable.
				then
					echo Writing . . . 
				else
					echo "$reference" is not writable.  Would you like to make it writable?
					read write;
					if [[ "$write" = y ]]
					then
						chmod 700 "$reference"
					elif [[ "$write" = n ]]
					then
						echo ok.
						continue
					elif [[ -z "$write" ]]
					then
						echo Please enter y or n.
					else
						echo Please enter y or n.
					fi
				fi
				if [[ -f $reference ]]									#check if user typed an existing file.
				then
					print -n "Enter link description: "
					read description
					if [[ -z "$description" ]]
					then
						description=$i
					fi
					 awk -v ref="$reference" -v desc="$description" '/<\/body.*>/ { print "<A HREF="ref">"desc"</A>" } { print }' "$reference" > tmp && mv tmp "$reference"
				else
					echo $reference is not a file, the option none, nor the enter key.
				fi
			fi
		done
	fi														#END Placing the link.

---------- Post updated at 03:41 PM ---------- Previous update was at 03:40 PM ----------

Code:
sed 's/<\/body>/<A HREF='"$index"'>'"$description"'<\/A>\
<\/body>/' "$index"

This is where the sed command is in the previous bit of code.
# 4  
Old 04-02-2012
I think I figured it out. You're using / as the separator, so when your URL contains / , that mangles the expression.

Use something else instead, like #

Code:
sed 's#</body>#<A HREF='"$index"'>'"$description"'</A>\
</body>#' "$index"

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-02-2012
yeah, its not doing that either.

---------- Post updated at 03:50 PM ---------- Previous update was at 03:49 PM ----------

It displays when used at command prompt, not when in the program.

---------- Post updated at 03:53 PM ---------- Previous update was at 03:50 PM ----------

ok, i'll try it out.

---------- Post updated at 03:57 PM ---------- Previous update was at 03:53 PM ----------

your a genius!

---------- Post updated at 03:58 PM ---------- Previous update was at 03:57 PM ----------

should have known better. ran into that problem before, then i guess got confused when i was using " " to cover spaces in variable name, thinking that covered all problems. Thanks!
This User Gave Thanks to robin_simple For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding text from a variable using sed (Or awk) with punctuation

Hi All, I would have though this would have been simple, but... I have text in a variable that I need to insert into a bunch of other files... The text is simple: ... (2 Replies)
Discussion started by: joeg1484
2 Replies

2. Shell Programming and Scripting

Adding more text to a variable

Is there a more simple way to add text to a variable I havevar="blue" and like to add green to getecho $var blue green Here is how I do it. var=$(echo "${var} green") ---------- Post updated at 13:23 ---------- Previous update was at 13:12 ---------- :o Uff Not always easy to see the... (2 Replies)
Discussion started by: Jotne
2 Replies

3. Shell Programming and Scripting

adding text before and after a word

Hello, I have a list: i.e: 40331786 50331787 30331788 30331789 30331790 50331791 50331792 50331793 70331794 70331795 70331796 ... ... and I need to add text before and after each number: (2 Replies)
Discussion started by: drbiloukos
2 Replies

4. Shell Programming and Scripting

Adding Text To |bc Output

Hello. I have this.. $ echo '2+2' | bc 4 But I want to output it as 4 MB So, can I achieve that through one single command line? Thank you. (7 Replies)
Discussion started by: aimy
7 Replies

5. Shell Programming and Scripting

Adding text into file

Hello, I after some assistance please. Below is a file I need to read and for each line write an output. My input file looks like this 2 20008 2003-08-26 2 20032 2003-08-26 2 20041 2003-08-26 2 20037 2003-08-26 2 20050 2003-08-26 6 ... (6 Replies)
Discussion started by: giles.cardew
6 Replies

6. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

7. Shell Programming and Scripting

adding text to a file

Hi All I am making a script in which.I have a problem i try to discribe. File1 content need to be append in file 2 but such as if content already exist then dont add. File1 IS Like this myname yourname hername hisname File2 %AddNameHere myname yourname hername hisname (3 Replies)
Discussion started by: aliahsan81
3 Replies

8. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

9. Shell Programming and Scripting

sed for adding word

hello i have a file (myfile) contains line as follows /home/mytarget/myproject i want to add a pattern at the end of this line. my pattern is- /.*mk i want like it - /home/mytarget/myproject/*.mk i tried sed like sed 's/$//*.mk/' myfile > newfilename, but not wrking. pls... (2 Replies)
Discussion started by: shailesh_arya
2 Replies

10. UNIX for Dummies Questions & Answers

sed - adding new line

I want to use sed to look for spaces in text and when find one move the next word to the next line. I used: sed 's/ /\n/g' out > new However when there is more than one space between two words it adds more lines between them. And I just want the words to be one under another. How can I... (2 Replies)
Discussion started by: sovixi
2 Replies
Login or Register to Ask a Question