HardwareHeaven.com

HardwareHeaven.com

Looking for the skin chooser?
 
 
  • Home

  • Hardware reviews

  • Articles

  • News

  • Tools

  • Gaming at HardwareHeaven

  • Forums

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Linux Operating Systems


Linux Operating Systems Talk about all Linux distributions and software here!

Reply
 
Thread Tools
Old Feb 24, 2005, 12:52 AM   #1
DriverHeaven Junior Member
 
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0
buchman is on a distinguished road

Any Help would be appreciated

I've got this question for some coursework and i'm stuck, didn't know which forum to post, but the linux one looked best as its unix commands

Q.
Write a pipeline that updates the content of a file (a file of your choice) by replacing all matches of the word ‘and’ with the symbol ‘&’ and by replacing the end of each line with “your name”. If the command completes successfully it should print ‘YES’, otherwise it should print ‘NO’.

I've got to write it using standard linux/unix commands and i'm stuck

As in the title any help would be appreciated.



also does anyone know what 'grep 1' does i've read the help, but still don't have a clue

Last edited by buchman; Feb 24, 2005 at 01:10 AM.
buchman is offline   Reply With Quote


Old Feb 24, 2005, 10:30 AM   #2
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

EEK!

Code:
[ -f ./foo -a -s ./foo ] && sed -i 's/and/\&/g;s/$/'`whoami`'/g' ./foo && echo YES || echo NO
Now, lets see what we do
[ -f ./foo -a -s ./foo ] tests if the ./foo exists and is a regular file and is not zero length
&& means if the previous statement is true then
sed - we'll do that later
|| means if anything prior is not true then

So in simple terms
"If the file ./foo exists and has content then run our sed statement and echo YES otherwise echo NO"

We do this as your argument must append your name to the send of each line - so it always should print YES unless it's an empty file or does not exist

Now for our sed statement - sed is a stream editor which is perfect for our needs. However, sed is very simple and yet very comples - here's a quick rundown on what it does

-i means edit file
(we could use -e to output file with our changes instead of editing it for testing)
s/foo/bar/g - this is a command that tells sed to replace the word "foo" with the word "bar". However, if the source had barfoobar in then barbarbar would be the result. The g bit on the end means global (ie every occurance)
The & symbol needs to be escaped so we use a \ to escape it (other chars need to be escaped like this too, like quotes, brackets, etc)
$ means "end of line"
`whoami` is a unix command that returns the name of the current user which we've embedded into our command

Hopefully this is the answer you need
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Old Feb 24, 2005, 10:34 AM Threadstarter Thread Starter   #3
DriverHeaven Junior Member
 
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0
buchman is on a distinguished road

well, WOW, and the explanation helps a lot too thank you gonna test it now.
buchman is offline   Reply With Quote
Old Feb 24, 2005, 11:14 AM   #4
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

Uh - I forgot to say that the ; char is a sed command seperator - it seperates our two substitution commands.
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Old Feb 25, 2005, 08:29 AM Threadstarter Thread Starter   #5
DriverHeaven Junior Member
 
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0
buchman is on a distinguished road

Thanks for the help that worked perfectly, you don't happen to know what 'grep 1' would do?
buchman is offline   Reply With Quote
Old Feb 25, 2005, 09:53 AM   #6
md5
DriverHeaven Senior Member
 
md5's Avatar
 
Join Date: Dec 2004
Location: Inside DriverHeaven
Posts: 856
Rep Power: 0
md5 has a spectacular aura aboutmd5 has a spectacular aura about

Quote:
Originally Posted by buchman
you don't happen to know what 'grep 1' would do?
Search its man page (type man grep) or have a look here

You can search for many Linux man pages online here

In short, grep 1 will filter all the output you feed it and will return you all the lines that contain the number "1" in them
__________________
md5 is offline   Reply With Quote
Old Feb 25, 2005, 12:16 PM   #7
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

BTW, what I did isn't really a pipeline. You could use pipes if you wanted too, but this way is much more efficient
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Reply

Thread Tools