ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
You could use sed.
sed 's/\\textit{\([^}]*\)}/\1/g' oldfile >newfile
This will remove the \textit from your file if it isn't split up between two or more lines.
>cat sample
This is a test. \textit{This is a sample line of Latex}. This is more on the line.
This is a second line.
This is the third line.
This \textit{italic text is
divided on two lines.}
This is a line without italic text.
sed -e 's/\\textit{\([^}]*\)}/\1/g' -e '/\\textit{\([^}]*\)$/{ N;' -e '/}/s/\\textit{\([^}]*\)}/\1/}' sample
This is a test. This is a sample line of Latex. This is more on the line.
This is a second line.
This is the third line.
This italic text is
divided on two lines.
This is a line without italic text.
The above example handles \textit{ .* } split up on two lines. For three lines you will need to use branching and use a sed program instead of '-e' on a oneliner:
sed -f sedprogram.sed sample >newsample
The above example will remove the \textit{ part even if there isn't a closing } so it isn't perfect. Also, for sed programs it is important to check if the pattern to be replaced works when it is on the last line.
Thanks for the replies.. The sed command worked great.
To get used to sed and know how to use it next time, I would like to understand the command you gave :
Quote:
sed 's/\\textit{\([^}]*\)}/\1/g' oldfile >newfile
As I'm not familiar with Regular expressions, I would like to understand this part : \([^}]*\
Anyone willing to explain ?
Thank you.
I'll begin with the "center" of that regular expression. The brackets [] specify a set of characters that the expression is supposed to match. But ^ as the first character in [] negates its content, i.e. we want to match all characters except those that are contained in [].
Thus [^}] matches all characters except a closing curly brace. We don't just want a single non-bracket character, but arbitrarily many, that's why [^}] is followed by *.
So far, \\textit{[^}]*} will match \textit followed by some text in curly braces, and we need to store the content of the brackets for later reference, that's why it has to be enclosed in \( .. \). Then we can use \1 to insert the first matching pair \( .. \) .
FYI, you might want to read the sed manual. Also, there is a "man regex" page which might help a little on understanding regular expressions. But a google search would return something more readable.
Also, if you edit in vim, you can do the same thing with the command:
:s/\\textit{\(.*\)}/\1/
This will perform the replacement on the current line. For the entire document, :%s\\textit{\(.*\)}/\1/g
This won't work when you have your italic text split up in two or more lines.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.