LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-06-2007, 12:19 AM   #1
rgistered
Member
 
Registered: Jan 2006
Distribution: arch, CentOS
Posts: 83

Rep: Reputation: 17
need a script!


greetings, i need a script which can automatically does the following and yes this script exists:

"anything anything anything to anything\ anything\ anything"

like i want to add " \ " whenever there is space.

hope am clear in explaining my point.
Thx

Last edited by rgistered; 11-06-2007 at 12:21 AM.
 
Old 11-06-2007, 12:28 AM   #2
checkmate3001
Member
 
Registered: Sep 2007
Location: Folsom, California
Distribution: Ubuntu, Mint, Debian, Suse
Posts: 307

Rep: Reputation: 32
This sounds like homework.
 
Old 11-06-2007, 12:32 AM   #3
rgistered
Member
 
Registered: Jan 2006
Distribution: arch, CentOS
Posts: 83

Original Poster
Rep: Reputation: 17
hmmmmm, am here for help. If you can plz as am new to linux.
thx
 
Old 11-06-2007, 12:47 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"man sed" ...
 
Old 11-06-2007, 01:09 AM   #5
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
http://www.grymoire.com/Unix/Awk.html

kind of working one:
Code:
cat myfile | awk '  { gsub("/","whateveriwant") ; print $0 }'
 
Old 11-06-2007, 03:09 AM   #6
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
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.
 
Old 11-06-2007, 04:05 AM   #7
arub
LQ Newbie
 
Registered: Sep 2007
Location: Coimbatore , India
Distribution: Fedora
Posts: 16
Blog Entries: 2

Rep: Reputation: 0
try this.
if u want to have the \ before any character place it between that "[" and "]" in the following command


------------------------------------------------------------------

echo "anything anything anything to anything\ anything\ anything" |sed 's/[\ ]/\\\0 /g'

-------------------------------------------------------------------

now i am placing \ before two character's
first is : \
second id: SPACE
 
Old 11-06-2007, 04:22 PM   #8
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
Quote:
Originally Posted by Simon Bridge View Post
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. )
 
Old 11-06-2007, 05:24 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by frenchn00b View Post
http://www.grymoire.com/Unix/Awk.html

kind of working one:
Code:
cat myfile | awk '  { gsub("/","whateveriwant") ; print $0 }'
No need cat
Code:
 awk '{ gsub("/","whateveriwant");print}' myfile
 
Old 12-19-2007, 02:33 PM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
If using sed,
Code:
echo "anything anything anything"  | sed 's, ,\\ ,g'
would be a cleaner, prettier, & more accurate solution to the stated problem.

RTM sed
 
Old 12-19-2007, 05:16 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
No sed, no awk, no cat with 's="anything anything anything"; echo "${s// /\\ }"'
 
Old 12-20-2007, 07:51 AM   #12
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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?
 
Old 12-20-2007, 11:02 PM   #13
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
"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!

What happened to OP?
 
Old 12-21-2007, 12:28 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by archtoad6 View Post
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...)
 
Old 12-21-2007, 04:22 AM   #15
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Uhmmm. No. You're right. It's kinda slow.

Last edited by unSpawn; 12-21-2007 at 04:31 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration