Sponsored Content
Top Forums Shell Programming and Scripting sed scripts are not playing nicely! Post 302433348 by naphelge on Tuesday 29th of June 2010 09:37:15 AM
Old 06-29-2010
Quote:
1. Use awk, not sed
Hi cfaj,

I know there are better tools for doing what I want to achieve, however, I am not a programmer and my time is very limited. I feel comfortable enough with sed at the command line and can get most of what I want done hacking around with sed. But with so many sed commands to run for this little project I thought a script file would be a perfect timesaver, but is now turning into a huge time sync and headache, but well worth the effort if I can get things to run as desired.

So for this example I have an act and scene from a Shakespeare play I want to make readable, but not a resource hog for my aging mobile phone (I have tried viewing stright up HTML files on my phone but they are resource pigs and scroll so slowly, so my basic HTML reformatting makes reading/scrolling much nicer).

I am including snippets from all files to show before-sed script-desired results (that I have yet to achieve from the script file).

Shakespeare snippet:
Quote:
SLY
13 Third, or fourth, or fift borough, I'll answer him
14 by law: I'll not budge an inch, boy: let him come,
15 and kindly.

[Falls asleep.]

Wind horns. Enter a LORD from hunting, with his TRAIN.

LORD
16 Huntsman, I charge thee, tender well my hounds:
17 Brach Merriman, the poor cur is emboss'd;
18 And couple Clowder with the deep-mouth'd brach.
19 Saw'st thou not, boy, how Silver made it good
20 At the hedge-corner, in the coldest fault?
21 I would not lose the dog for twenty pound.

FIRST_HUNTSMAN
22 Why, Belman is as good as he, my lord;
23 He cried upon it at the merest loss
24 And twice today pick'd out the dullest scent:
25 Trust me, I take him for the better dog.

LORD
26 Thou art a fool: if Echo were as fleet,
Here I need to 1) delete all empty/blank lines; 2) get rid of all line numbers except ones that end with a fiver or zero; 3) and every 25th line make a hyperlinked anchor point for easy page navigation in my phone.

So here is what I have sofar for my sed script file to take care of the requirements mentioned above. Of course there is much yet to be added to this file to get the page totally formatted the way I want, but this is where I am stuck so no use in moving ahead, yet.

Code:
{

#REMOVE ALL UNWANTED WHITESPACE, BLANK LINES, TEXT, OR ANYTHING ELSE THAT MUST GO SECTION
		#delete all blank lines
	/^$/d
		#rm all beginning and trailing whitespace followed by all blank lines
	s/^[ \t]*//g
	s/[ \t]*$//g
		#remove an uwanted escaped return chars that will prevent the next line from applying correctly
	s/\r//g 

#SUBSTITUTION SECTION
		#subst. long hyphen with two dashes
	s/-/--/g
		#subst. single quote for proper single quote
	s/'/'/g
		#next two lines subst. improper double quotes for proper
	s/”/"/g
	s/“/"/g

#reformat line numbers onto their own lines and delete all lines not ending with a zero or five
	s/^.*[1-4]   \(.*\)$/\1/g
	s/^.*[6-9]   \(.*\)$/\1/g
#	s/^\([0-9]*\)   \([A-Za-z']\)/<tr><td>\1<\/td><\/tr>\n\2/g
	s/^\([0-9]*\)   \([A-Za-z']\)/\1\n\2/g
			
#HTML TAG SECTION
	s/^\(25\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(50\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(75\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(100\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(125\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(150\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(175\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(200\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(225\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(250\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(275\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(300\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(325\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(350\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(375\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(400\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(425\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(450\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(475\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^\(500\)$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
		#add hyperlinks to line numbers in denominations of 25 #DOES NOT WORK!!!!!!!!!!
	#/0<\/td>/s\(<tr><td>\)\(.*\)\(<\/td><\/tr>\)/\1<a href="#\2Top" name="\2"><u><b>\2<\/b><\/u><\/a>\3/g
	s/^<tr><td>\(25\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(50\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(75\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(100\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(125\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(150\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(175\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(200\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(225\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(250\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(275\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(300\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(325\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(350\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(375\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(400\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(425\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(450\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(475\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
	s/^<tr><td>\(500\)<\/td><\/tr>$/<tr><td><a href="#\1Top" name="\1"><u><b>\1<\/b><\/u><\/a><\/td><\/tr>/g
		#enclose any lines with names in UPPERCASE in bold tags
	s/^\([A-Z_]\{,20\}\)$/<tr><td><b>\1<\/b><\/td><\/tr>/g
		#eclose any lines with sq. brackets already in bold
	/^\[.*\]/s/\(\[.*\]\)/<tr><td><b>\1<\/b><\/td><\/tr>/g
		#enclose all lines of action in bold and sq. brackets
	/Enter/s/^\(.*\)$/<tr><td><b>\[\1\]<\/b><\/td><\/tr>/g
	/Exit/s/^\(.*\)$/<tr><td><b>\[\1\]<\/b><\/td><\/tr>/g
		#enclose all lines beginning with numbers in tags
	
	
}

Once again, I am absolutely convinced there are better, more appropriate tools to accomplish what I want, but presently and for the forseeable future sed is the tool I have at my disposal, which should be able to take care of my requirements once I figure out why things are not running in the script file as I expect.

The end result should look like:
HTML Code:
<tr><td><b>SLY<\/b><\/td><\/tr>
Third, or fourth, or fift borough, I'll answer him
by law: I'll not budge an inch, boy: let him come,
<tr><td>15<\/td><\/tr>
and kindly.
<tr><td><b>[Falls asleep.]<\/b><\/td><\/tr>
<tr><td><b>[Wind horns. Enter a LORD from hunting, with his TRAIN.]<\/b><\/td><\/tr>
<tr><td><b>LORD<\/b><\/td><\/tr>
Huntsman, I charge thee, tender well my hounds:
Brach Merriman, the poor cur is emboss'd;
And couple Clowder with the deep-mouth'd brach.
Saw'st thou not, boy, how Silver made it good
<tr><td>20<\/td><\/tr>
At the hedge-corner, in the coldest fault?
I would not lose the dog for twenty pound.
<tr><td><b>FIRST_HUNTSMAN<\/b><\/td><\/tr>
Why, Belman is as good as he, my lord;
He cried upon it at the merest loss
And twice today pick'd out the dullest scent:
<tr><td><a href="25Top" name="25"><u><b>25<\/b><\/u><\/a><\/td><\/tr>
Trust me, I take him for the better dog.
<tr><td><b>LORD<\/b><\/td><\/tr>
Thou art a fool: if Echo were as fleet,
Like I said, with more sed scripts req'd to finish the desired formatting, but that is how it should look with the sed script at the point I pasted above.


I also noticed looking at a preview, a few lines in the sed script that are there as a result of trying a few different things to get things working nicely, so I hope they are not confusing. They should be fairly easy to follow I think?

thanks again,
tt
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

nicely formatted directory listing from batch ssh session

Hi, I am really struggling to finish of a script I have been assigned. The script's purpose is to log on to each server defined in an array, determine the Web Server version, and list the directory beneath the installation directory. In my case, this installation directory is almost always... (6 Replies)
Discussion started by: grebbux
6 Replies

2. Shell Programming and Scripting

tab string variables nicely

I'm using korn shell for my shell script I'm reading a file and get the data I want out and append it to a variable directly, it shows like this AutoMicroReport_53767.txt BANI BHUM (570N) 20080520 17:54 WAN HAI 306 078 ITH 1 1 HKHKG 20 GP M S06 7 6 1 0 TCYONG AutoMicroReport_53767.txt... (2 Replies)
Discussion started by: finalight
2 Replies

3. Shell Programming and Scripting

number sign(#) in sed scripts

Hi. In a simple sed script i have number signs - #. But i can't find any description on their functionality. One thing that i find is that # in sed scripts can mean comment if it is placed in the beginning of the line. I thought may be this undocumented(for me) behavior refers to regular... (1 Reply)
Discussion started by: kukuruku
1 Replies

4. UNIX and Linux Applications

Display mysql results nicely

Hi, perhaps this is a dumb question. I'm running queries on mysql and I'm getting tabbed results like these: mysql> SELECT * from metrics_status WHERE date = '2012-03-30'; <TABLE... (1 Reply)
Discussion started by: erick_tuk
1 Replies

5. Shell Programming and Scripting

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. I use the program to change an occurance of a word throughout different files that are being tested. At first i had to Create a new script,... (1 Reply)
Discussion started by: Johnny2518
1 Replies

6. Homework & Coursework Questions

Help with Shell Scripts Using sed in multiple files.

Hi, I was hoping that someone could help me. I have a problem that i am trying to work on and it requires me to change text within multiple files using sed. What i have so far is !/bin/sh File1="$3" File2="$4" File3="$5" testNum="$File1" while test "$testNum" <= "$File3"; do echo... (12 Replies)
Discussion started by: Johnny2518
12 Replies

7. UNIX for Dummies Questions & Answers

sed command playing with me!

Hello guys, I was working with my data (a tab delimted text file) in Linux and wanted to replace all "NaN" values in my huge table with "NA", so i used the following sed command: sed 's/NaN/NA/g' /filepath -> outputfile.txt The results were good, all "NaN" were replaced with "NA", but the... (7 Replies)
Discussion started by: Error404
7 Replies

8. UNIX for Dummies Questions & Answers

Rename scripts using xargs/sed

Morning all I've got loads of scripts but the names are too long! I've stuck the list in a flat file (names) and I'm trying to read that in line by line and create the new names (in to directory new) from the list. It looks like this: xargs -n1 -I{} <names cat {} | sed... (1 Reply)
Discussion started by: Grueben
1 Replies

9. Homework & Coursework Questions

Asst: Using Shell Scripts with sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: One problem with the use of sed in simple text substitutions is that sed will intepret some characters as... (1 Reply)
Discussion started by: raymondbn
1 Replies
All times are GMT -4. The time now is 10:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy