String editing using sed? awk?

Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions String editing using sed? awk?
# 1  
Old 11-11-2009
String editing using sed? awk?

1. The problem statement, all variables and given/known data:

Problem Statement for project:

When an account is created on the CS Unix network, a
public html directory is created in the account's home directory. A default web page is put into that directory.

Some users replace or edit the default page, while others do not. We would like to add a new link from the department web page to another page which lists all students who have changed their web page from the default. We don't want to track these things by hand, and we want the list to be automatically updated every night at 3:00AM.

You are to write a script that will
1. find all users who are students and who have changed their web page from the default that
was provided when their account was created,
2. generate an HTML file named student web pages.html which contains a nicely formatted list of links to each student web page that you found,
3. copy that file into a directory specified on the command line, and

4. make sure that anyone can read it.

My Problem:

I have edited the output of the above find command to a file that has lines that look like this:

<p><a href="">Link</a>
<p><a href="">Link</a>
<p><a href="">Link</a>

What I need it to do now is take that line and turn it into this:

<p><a href="">name</a>
<p><a href="">name2</a>
<p><a href="">nameasdas</a>

I have tried using sed and just can't figure it out. Can anyone help?

2. Relevant commands, code, scripts, algorithms:

Sed ?? awk??

3. The attempts at a solution (include all code and scripts):

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Texas Tech University
Lubbock TX
Dr. Pyeatt
CS 3352

Thanks for the help.
# 2  
Old 11-11-2009
First off, your estimation about sed being the right tool is correct. It was written with problems like yours in mind.

BUT: i don't think your are on the right track. The reason is: you create a sort of list and this list is almost correct - almost, but not quite so. Now you want to patch this almost-correct list into something which is indeed correct. The problem is: when you are writing software it is most of the times advisable to do it correctly the first time instead of doing it almost correctly and then patch it.

Instead of creating an sed-script to "patch the list to work" create the list correctly immediately. This is easily possible.

You mention an "above find command", but i cannot find that. So: why don't you put your script so far here and we help you to create not an amost-correct but a correct list.

Btw., i have a suspicion that your find-command (if you have used that alone) will not be giving the correct result either because i don't know of any way to solve your problem (even to the almost-correct state you have) with "find" alone - this would be a rather non-trivial task if it could be done at all (and i suspect it is impossible). This also could be corrected if you provide your work so far.

I hope this helps.

# 3  
Old 11-11-2009
Here is my code that I have written so far:

find ./*grad*/*/public_html/index.html -newer /etc/skel/local.cshrc > undergrad1/peage/files


cat files | sed 's#\./grad[1-5]/#\<p\>\<a href\=\"http://www.cs\.ttu\.edu/\~#g' > ./links

cat links | sed 's#\./undergrad[1-5]/#\<p\>\<a href\=\"http://www.cs\.ttu\.edu/\~#g' > ./links

cat links | sed 's#/public_html/index\.html#\"\>Link\</a\>#g' > ./links

echo '<html>
<body>' > foo1

cat links > foo2

echo '</body>
</html>' > foo3

cat foo1 foo2 foo3 > ./public_html/test.html

The find command is just giving me the path of the user who's index.html is edited, and I am trying to transform that into the links I stated above. My problem is that the users name is only listed once in the path and when I am using sed I do not know how to copy just their user name(lengths varying) to be used twice in the same line.

Thanks agian for the help
# 4  
Old 11-11-2009
Can you post a snippet of lines in the file "files" which you cat in the first line to get the file links

# 5  
Old 11-11-2009
Ok, this is about the script i expected. It has several problems, but first, lets analyze your problem correctly. Software engineering (and script programming is a form of software engineering) is about correctly analyzing the problem before you envision a solution.

Your "find" command compares dates - the date/time the file local.cshrc has been changed to when the file index.html in the users home directory. If a new user is created after the last change of the skeleton file his file ~/index.html will be newer regardless of being changed or not. On the other hand if the skeleton file is changed even the files reported as changed before will be reported as not changed. So you don't find the correct files in first place.

Lets see: when a new user is created s/he is delivered some standard version of index.html. If s/he changes it it will differ from this standard file, if not then not. Now this is indeed a criterion which will pick out the right files, yes?

So, your task is to find a unix utility which compares two files and finds out if they differ or not. If there is such an utility we could write the following program (in pseudocode):

while (cycle through all users)
     if (~thisusers/index.html is equal  to the standard index.html)
          do nothing
          write a corresponding line to your output file

There are two things you can do for now:

1) find a way to get all the users names. (You will need this to set up the while-loop above.) I could tell you, but i don't want to spoil your fun finding it out.

2) find a unix command which compares two files and finds out if they are equal (you will need this for deciding the if-construction).

Report back here with your answers and i will give you the directions for the next stage.

Sorry for dragging this out this way, but i cannot give you enough directions at once without giving away the solution. As the spirit of this board is to help you learn and not spoon-feed you solutions we have to do it this way. As i am convinced you will soon find out Unix is not only a lot of fun to work with but also a lot of fun to learn about, so enjoy it while the learning experience lasts.

# 6  
Old 11-11-2009
Thanks alot, and I would much rather learn myself than be spoon fed since I like this kind of stuff. Thanks again and I will see what I can do.

---------- Post updated at 07:19 PM ---------- Previous update was at 07:01 PM ----------

Ok im already having trouble. I can create a file that lists all the users; I know that I should use cmp to tell if the files are the same, but I do not know how to set up the while loop take each username in the file and then use it in the iff statement. I know I have to use a variable, but how do I get the while loop to transverse the file line by line?
# 7  
Old 11-12-2009
Assume users are in a file named users

for user in `cat users`; do echo $user; done

replace echo $user with the functionality you intend

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

sed awk to remove the , in a string

Dear All, Can anyone help to remove the , bewteen "" in a string by using sed or awk? e.g. input : 1,4,5,"abcdef","we,are,here",4,"help hep" output:1,4,5,"abcdef","wearehere",4,"help hep" Thanks, Mimi (5 Replies)
Discussion started by: mimilaw
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

editing file with awk cut and sed

HI All, I am new to unix. I have a file would like to do some editing by using awk, cut and sed. Could anyone help? This file contain 100 lines. There are one line for example: 2,"102343454",5060,"579668","579668","579668","SIP",,,"825922","035885221283026",1,268,"00:59:00.782 APR 17... (2 Replies)
Discussion started by: mimilaw
2 Replies

6. Shell Programming and Scripting

match string exactly with awk/sed

Hi all, I have a list that I would like to parse with awk/sed. The list is contains entries such as: JournalTitle: Biochemistry JournalTitle: Biochemistry and cell biology = Biochimie et biologie cellulaire JournalTitle: Biochemistry and experimental biology JournalTitle: Biochemistry and... (6 Replies)
Discussion started by: euval
6 Replies

7. Shell Programming and Scripting

Line/Variable Editing for Awk sed Cut

Hello, i have a file, i open the file and read the line, i want to get the first item in the csv file and also teh third+6 item and wirte it to a new csv file. only problem is that using echo it takes TOO LONG: please help a newbie. below is my code: WorkingDir=$1 FileName=`cut -d ',' -f... (2 Replies)
Discussion started by: limamichelle
2 Replies

8. Shell Programming and Scripting

sed or awk editing help

Hi all I use aix (sadly). I've got a file consisting of fields separated by commas, I need a sed or awk command that will delete all spaces between two commas as long as there are only spaces between the commas. eg ,abc, ,sd , ,dr at would become ,abc,,sd ,,dr at I have... (53 Replies)
Discussion started by: mychmose
53 Replies

9. Shell Programming and Scripting

awk/sed - getting string instead of number

Hi! I am writing a script handling downloading list of files and I have to check whether file is present locally and if not finished than continue downloading. To do so I have to compare sizes of remote file and local file. To check remote file size I have to parse something like this: ... (2 Replies)
Discussion started by: hrwath
2 Replies

10. Shell Programming and Scripting

Editing File using awk/sed

Hello Awk Gurus, Can anyone of you help me with the below problem. I have got a file having data in below format pmFaultyTransportBlocks ----------------------- 9842993 pmFrmNoOfDiscRachFrames ----------------------- NULL pmNoRecRandomAccSuccess -----------------------... (4 Replies)
Discussion started by: Mohammed
4 Replies
Login or Register to Ask a Question