LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2007, 09:56 PM   #1
hammertime1983
LQ Newbie
 
Registered: Mar 2007
Posts: 15

Rep: Reputation: Disabled
Search and Replace from bash


Hello,

I'm looking for a way to search for blank space and replace it with a comma while working from within bash, i.e. I can't open VI.
 
Old 11-01-2007, 10:50 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Blank space in a command that you have typed or in a file?
 
Old 11-02-2007, 01:04 AM   #3
SqdnGuns
Member
 
Registered: Aug 2005
Distribution: Slackware64 14.2 w/ CSB & MSB
Posts: 998

Rep: Reputation: 96
#!/bin/bash
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
j=$((j-1))
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
do
for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
do
newname=`echo "$name" | tr " " ","`
echo "$name" "$newname"
mv "$name" "$newname"
done
done
##########

That should do it for you. Save it as a text file, blah.sh, make it executable and run it in the directory you want. Be careful where you run it...........don't want rename any "system" files.

Last edited by SqdnGuns; 11-02-2007 at 04:17 PM.
 
Old 11-02-2007, 08:23 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
SqdnGuns, please post code in [code] tags to improve readability.
 
Old 11-02-2007, 04:18 PM   #5
SqdnGuns
Member
 
Registered: Aug 2005
Distribution: Slackware64 14.2 w/ CSB & MSB
Posts: 998

Rep: Reputation: 96
Quote:
Originally Posted by matthewg42 View Post
SqdnGuns, please post code in [code] tags to improve readability.
Yup, I should have known better.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
Search-replace in code fuzzyBuzz Programming 12 10-15-2007 11:37 AM
search/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 10:01 AM


All times are GMT -5. The time now is 07:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration