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.
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?
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. )
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.
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.
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
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.