LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 08: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, 09:50 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Blank space in a command that you have typed or in a file?
 
Old 11-02-2007, 12:04 AM   #3
SqdnGuns
Senior Member
 
Registered: Aug 2005
Location: Pensacola, FL
Distribution: Slackware64® Current & Arch
Posts: 1,092

Rep: Reputation: 174Reputation: 174
#!/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 03:17 PM.
 
Old 11-02-2007, 07:23 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
SqdnGuns, please post code in [code] tags to improve readability.
 
Old 11-02-2007, 03:18 PM   #5
SqdnGuns
Senior Member
 
Registered: Aug 2005
Location: Pensacola, FL
Distribution: Slackware64® Current & Arch
Posts: 1,092

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


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 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 12:13 AM
Search-replace in code fuzzyBuzz Programming 12 10-15-2007 10:37 AM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 09:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:48 PM.

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