LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-31-2009, 01:11 PM   #1
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Rep: Reputation: 15
I need a quote for this please


Hi everyone,

I have several hundred .html files that have a mistake that I would like to fix. Can someone please help me with the code to do the change in all the files at once? I would hate to spend hours doing the change manually. Thank you.

So, as an example, how would you change the following in all the files in the same directory?

Before:
<a href="http://www.googl.com">Google</a>

After:
<a href="http://www.google.com">Google</a>

Thanks a million.
 
Old 03-31-2009, 01:17 PM   #2
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
I just realized that I put quote in the subject, I meant "code". I really don't want to change hundreds of files manually, I can barely type a correct subject line. )
 
Old 03-31-2009, 01:23 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
This may work (you are advised to test it yourself before running it on real files, though):

Code:
for file in `ls *`
do
  sed -i 's/googl/google/' $file
done
It should replace all instances of googl with google in all the files in the output from ls *.
 
Old 03-31-2009, 02:18 PM   #4
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by Nylex View Post
This may work (you are advised to test it yourself before running it on real files, though):

Code:
for file in `ls *`
do
  sed -i 's/googl/google/' $file
done
It should replace all instances of googl with google in all the files in the output from ls *.
If it's only html files you can...

Code:
for file in $(ls -1 *.html)
do
  sed -i 's/googl/google/' $file
done
-C
 
Old 03-31-2009, 02:36 PM   #5
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks a lot to both of you. I'll test these out when I get home tonight.
 
Old 03-31-2009, 05:00 PM   #6
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
I have one more request...been searching for a solution to this on the net, but couldn't find a solution that matched my problem exactly.

The problem is that I have a few hundred files in the same directory. I would like to change a part of their names. So I would like to change:

My-Super-File-Name.html

To:

My-Great-File-Name.html

Can you please tell me how this is done? Thanks in advance.
 
Old 03-31-2009, 05:29 PM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by shahgols View Post
I have one more request...been searching for a solution to this on the net, but couldn't find a solution that matched my problem exactly.

The problem is that I have a few hundred files in the same directory. I would like to change a part of their names. So I would like to change:

My-Super-File-Name.html

To:

My-Great-File-Name.html

Can you please tell me how this is done? Thanks in advance.
If you are using Debian or a Debian derivative, the rename command takes Perl regular expressions and can do this pretty easily:
Code:
rename 's/Super/Great/' *
 
Old 03-31-2009, 05:47 PM   #8
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
wonderful, thank you. I am on Vector Linux and it seems like rename works differently here:

rename Super Great *.html

Thanks again.
 
Old 03-31-2009, 05:57 PM   #9
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
ARRRGHHHH, the original code doesn't work. I guess the example that I gave was not exactly right. Here's the exact code that I want to change inside my file:

<h1><a href="#">The one and only</a></h1>

To:

<h1>The one and only</h1>

I guess the slashes are throwing sed off, because I get an error. How do I do this? Thanks again.
 
Old 03-31-2009, 06:16 PM   #10
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
Quote:
Originally Posted by shahgols View Post
I guess the slashes are throwing sed off, because I get an error. How do I do this? Thanks again.
If you use forward-slashes as the delimiters in sed, and the slashes appear in the pattern as well, you need to escape them with a backslash, but you can use a large range of other delimiters (just choose one you don't need to use in the pattern) - e.g.:

Code:
sed -i 's/\(<h1>\)\(<a[^>]*>\)\([^<]*\)\(<\/a>\)\(<\/h1>\)/\1\3\5/' $file
Or:

Code:
sed -i 's!\(<h1>\)\(<a[^>]*>\)\([^<]*\)\(</a>\)\(</h1>\)!\1\3\5!' $file
Here, exclamation marks are used as the delimiters, so the backslashes in </a> and </h1> are not needed. The \(...\)'s define sub-patterns, that are then replayed by \1 (first sub-pattern), \3 (third sub-pattern), etc.
 
Old 03-31-2009, 07:03 PM   #11
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
Thank you Rob, but to be honest, to me, this is like Chinese mixed in with some English. What should I be studying to learn these stuff? Bash programming?

Anyhow, I ran the following command and got an error.

Code:
root:# for file in 'ls *.html'
> do
> sed -i 's!\(<h1>\)\(<a[^>]*>\)\([^<]*\)\(</a>\)\(</h1>\)!\1\3\5!' $file
> done
sed: can't read ls: No such file or directory
Any idea what I need to do to fix this?
 
Old 03-31-2009, 07:47 PM   #12
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
I know what you mean - I often refer to these patterns as an explosion in a punctuation factory. They are called regular expressions, and consist of literal characters and metacharacters. The pattern means:
Code:
!             Delimiter beginning search pattern
\(<h1>\)      Group 1: literal <h1>
\(<a[^>]*>\)  Group 2: <a then 0 or more characters that are not >'s, then >
\([^<]*\)     Group 3: Any number of characters that are not <'s
\(</a>\)      Group 4: literal </a>
\(</h1>\)     Group 5: literal </h1>
!             End of search pattern, beginning of replace pattern
\1            Replay group 1
\3            Replay group 3
\5            Replay group 5
!             End of replace pattern
Some stuff on sed and regular expressions from the Linux documentation project.

The reason you got the error message is that you used single quotes - '...' - rather than backticks -`...` around the ls command in the first line - very easy to do. Backticks are on the key to the left of the 1 on a US/UK keyboard, but I would tend to use $(ls *.html) - the $(...) does the same thing as the backticks, but is a lot easier to read.

Edited to add: slight warning about the documentation on regular expressions - there are different forms. For instance, in the link above, it tells you parentheses -- ( ) -- enclose a group. However, in the sed version, escaped parentheses - \( \) - are used.

Last edited by Robhogg; 04-01-2009 at 01:53 PM. Reason: compliance with English 1.0
 
Old 03-31-2009, 08:10 PM   #13
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
Wow, thank you so much Rob for helping me learn this stuff.

I'll try the command again shortly. Right now I got to get some dinner. Peace and thanks again.
 
Old 03-31-2009, 11:18 PM   #14
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by shahgols View Post
Thank you Rob, but to be honest, to me, this is like Chinese mixed in with some English. What should I be studying to learn these stuff? Bash programming?

Anyhow, I ran the following command and got an error.

Code:
root:# for file in 'ls *.html'
> do
> sed -i 's!\(<h1>\)\(<a[^>]*>\)\([^<]*\)\(</a>\)\(</h1>\)!\1\3\5!' $file
> done
sed: can't read ls: No such file or directory
Any idea what I need to do to fix this?
I see the problem...you put ' instead of `

This is why I always use $(command) instead of `command` it's easier to read (besides...using ` is deprecated...)

Try it this way...
Code:
root:# for file in $(ls *.html)
> do
> sed -i 's!\(<h1>\)\(<a[^>]*>\)\([^<]*\)\(</a>\)\(</h1>\)!\1\3\5!' $file
> done
-C

Last edited by custangro; 03-31-2009 at 11:20 PM.
 
Old 04-01-2009, 12:01 PM   #15
shahgols
Member
 
Registered: Dec 2006
Posts: 97

Original Poster
Rep: Reputation: 15
Thank you so much everyone, this worked and saved me HOURS of time! So great, thank you!
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
when to quote or not to quote variable names - mktemp problem glinux Linux - Newbie 3 02-20-2009 04:37 PM
Price quote... jsm Linux - General 2 11-08-2006 02:20 PM
Boot Quote? liquidrabbit Slackware 3 04-10-2006 05:37 PM
Quote 1 gb to 1 user? itz2000 Linux - Newbie 5 02-19-2006 02:30 PM
quote tundra General 2 09-21-2002 05:58 AM


All times are GMT -5. The time now is 01:25 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration