|
|||||||
| Linux Operating Systems Talk about all Linux distributions and software here! |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
DriverHeaven Junior Member
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0 ![]() |
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. |
|
|
|
|
|
#2 |
|
A Legend in Underwear
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0 ![]() |
Code:
[ -f ./foo -a -s ./foo ] && sed -i 's/and/\&/g;s/$/'`whoami`'/g' ./foo && echo YES || echo NO [ -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 |
|
|
|
|
|
|
|
DriverHeaven Junior Member
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0 ![]() |
well, WOW, and the explanation helps a lot too
thank you gonna test it now.
|
|
|
|
|
|
#4 |
|
A Legend in Underwear
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0 ![]() |
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 |
|
|
|
|
|
|
|
DriverHeaven Junior Member
Join Date: Mar 2003
Location: London, UK
Posts: 44
Rep Power: 0 ![]() |
Thanks for the help that worked perfectly, you don't happen to know what 'grep 1' would do?
|
|
|
|
|
|
#6 | |
|
DriverHeaven Senior Member
Join Date: Dec 2004
Location: Inside DriverHeaven
Posts: 856
Rep Power: 0 ![]() ![]() |
Quote:
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
__________________
|
|
|
|
|
|
|
#7 |
|
A Legend in Underwear
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0 ![]() |
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 |
|
|
|
![]() |
| Thread Tools | |
|
|