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.
I have a sed script to search and replace a pattern on the next kind of text:
Code:
C/username/Mydocuments/games & music
C/username/Mydocuments/New files 09-17-2007
C/username/settings
The script is:
Code:
sed 's/\([^/]*\/[^/]*\/\).*$/\1New String/g' inputfile
The script search strings with 3 or 2 subfolders level and replaces with "New string "as follow: If has 3 subfolders, the script replaces last 2 subfolders
from:
Code:
C/username/Mydocuments/games & music
C/username/Mydocuments/New files 09-17-2007
to
Code:
C/username/New String
C/username/New String
If has 2 subfolders, the script replaces last subfolder from
Code:
C/username/settings
to
Code:
C/username/New String
The last directory strings belongs to column 3 within inputfile, and I would like to
do the same job(search and replace on 3rd column) with awk, but using the same regex it doesn´t work.
I´ve tried two ways, in both get errors:
Code:
1-) awk -F"|" '{gsub(\([^/]*\/[^/]*\/\).*$,"New string",$3};print}' infile
awk: {gsub(\(.*\/\)[^/]*$,"New string",$3};print}
awk: ^ backslash not last character on line
and
2-) awk -F"|" '{gsub(([^/]*\/[^/]*\/).*$,"New string",$3};print}' infile
awk: {gsub(([^/]*\/[^/]*\/).*$,"New string",$3};print}
awk: ^ syntax error
awk: fatal: Invalid regular expression: /]*\/[^/
Maybe somebody give me some help, in which would be the correct regexp to include with awk script to do the same task.
*I´m using Cygwin.
Thanks in advance,
Regards,
Click here to see the post LQ members have rated as the most helpful post in this thread.
Actually I´m trying to do it now using awk and its gsub function, because the file now has 40 columns separeted by "|", and I would like to operate only in 3erd column.
the only difference is that you must not escape the parentheses and the matched text is evaluated as \\1 in the replacement. Note that this function is available only in GNU awk: if compatibility to other awk implementations is an issue, you cannot use gensub.
Thanks for share that and for your explanation, I didn´t know about gensub, and now I understand better why the same regexp wasn´t working in awk for me. It works nice in my cygwin.
One question,
Running your script, but replace the pattern with nothing (""), remains a "/" at the end of 3rd column, I now how to delete it with sed using "sed 's/\/$//'". But only if is applied to last column, otherwise the rest of columns are deleted. In order to do not affect other columns information, is better do it with awk I think.
What code is needed to add to your script to delete the remaining "/" when I replace the pattern with "" nothing?
Thanks, thanks colucix. Works great. Something more I´ve learned today!
One more question, maybe somebody knows.
What kind of regexp are used in command shell, for instance, used by awk or sed? Is Perl style or another variant?
I ask this because the regexp used in this problem certainly works with sed and with a little modfication in awk too, but when I test the regexp in a freeware utility like RegExr to test regexp expressions, this regexp doesn´t work.
Regular-expression libraries do (unfortunately...) vary somewhat, although most of them have followed Perl's lead.
Speaking of Perl... It certainly seems to me that Perl might be "the cat's meow" in this situation. It might well be "the right tool for the job."
In the Unix|Linux environments, you have "an embarrassment of riches" with regard to the choice of tools that you have at your disposal (all of them "for free"). It is therefore particularly important to choose what is the appropriate one. For you. For this task. (And as the Perl folks love to say: "TMTOWTDI = There's More Than One Way To Do It.")
I personally think, for example, that lots of folks do amazing things with "shell scripts," even though "shell scripting" was never really designed to do the things that they are managing to do with it. So... are they right or are they not? (You decide.)
Anyhow... always be mindful that you might inadvertantly be using a wrench to drive a nail. Also, always be very mindful that, no matter what you are setting out to do, you are not the first person to have done so. "The time that you unnecessarily wasted with a computer could have been time that you spent drinking a nice celebratory beer."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.