Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
checkmate3001 comments: "This sounds like homework."
rgistered replies, "hmmmmm, am here for help."
... that's not a denial man.
I suggest you look over your course-notes again.
If this is not homework, you can prove it by telling us the purpose of the script. What does this task achieve that is useful to you?
Note: this is "help". It won't help anyone if we do your homework for you. Encouraging (forcing) you to do your own homework is the best help we can offer. OTOH: you could show that you have genuinely tried... show the reasoning you have used... then, perhaps we can tell you where you are going wrong. That way you learn more, see.
checkmate3001 comments: "This sounds like homework."
rgistered replies, "hmmmmm, am here for help."
... that's not a denial man.
I suggest you look over your course-notes again.
If this is not homework, you can prove it by telling us the purpose of the script. What does this task achieve that is useful to you?
Note: this is "help". It won't help anyone if we do your homework for you. Encouraging (forcing) you to do your own homework is the best help we can offer. OTOH: you could show that you have genuinely tried... show the reasoning you have used... then, perhaps we can tell you where you are going wrong. That way you learn more, see.
It might be.
awk is my favourite, put down the code, we can correct/fix it.
( Good luck with sed, I never understood anything after one year with all the bar stuffs. )
"Remember Alexander -- you don't have to untie the knot to solve the puzzle."
... though it does help if you have the worlds largest standing army outside...
Alexander: There, it's untied.
Official: Um, that's sort of, cheati...
Alexander: Look out that window would you...
Official: But a brilliant workaround anyway, Sire!
Elegant, ticks me off that it slipped my mind. Tnx for the reminder.
How well does it scale up -- i.e. process a whole file?
Code:
# head -10 file
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
anything anything anything
# wc -l file
1000000 file
# time sed 's, ,\\ ,g' file > sedtest
real 0m12.156s
user 0m11.945s
sys 0m0.160s
# time while read s;do echo ${s// /\\ };done < file > whilelooptest
real 2m8.980s
user 1m55.195s
sys 0m11.181s
# time awk 'BEGIN{OFS="\\ "}{$1=$1}1' file > awktest
real 0m2.018s
user 0m1.840s
sys 0m0.176s
#ls -l *test | awk '{print $5,$9}'
29000000 awktest
29000000 sedtest
29000000 whilelooptest
shell loops are the slowest compared to sed/awk, at least from my experiences. For processing large files with text manipulations, its still better to use awk. (or sed...)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.