[SOLVED] change the order of first name, last name with sed
Linux - NewbieThis 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
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.
Using \w for a character class is from perl. Perl's useful but since you are working with sed you need to use normal regular expressions. See "man 7 regex" for that. So with that in mind the sed program you show above could be tweaked into the following to work with the sample data you give:
You have several problems. Possibly the complexity is the main one as it stops you seeing the problems.
Try changing just one thing. Only. When that works, add another change. One simple change will expose a major over-sight you need to address first.
Excellent. Do poke around in "man 7 regex" and, maybe, "man perlre" Once they get more familiar it will be quite easy to work with. The former provides info about normal (POSIX) regular expressions. The latter provides info about perl regular expressions, which you will encounter nearly everywhere as perl-compatible regular expressions (PCRE).
In perl you could use the -p switch to loop through the data file just like with sed:
There's also a loop available with the -n switch for perl, too. See "man perlrun" for what -p and -n and -e do for perl. But if that's a distraction, focus on "man sed"
So shall I learn perl, too?
I just decided, to learn usage of regular expressions with sed and awk (a little bit difficult at first time).
With perl can I use regex and the usage will be easier?
Although perl is really very useful itself as a scripting language, it's not necessary to learn a lot of it in order to take advantage of its advanced regular expressions. Again, PCRE will turn up just about everywhere and not just in perl: The can be found in C, PHP, Python, Ruby, Java, etc. Further, perl itself is part of all operating systems, except of course that one line of junk that needs not be named.
So I'd say learn perl's regular expressions and remember the difference between them and POSIX regular expressions. Then if you find you are using one over the other more often, spend time on that one. I find them both useful even though I spent years not knowing sed and using perl when normally sed would be called for.
As for easier, that's hard to say. But you will get a good return on your efforts with PCRE.
PCRE is extremely powerful, and useful. It is something you should probably aspire to learning later.
For sed/grep, you will have enough intricacies to get to know. Stick with them for now, it will be less confusing.
Any regex can drive you crazy, no need to go running after it ...
So shall I learn perl, too?
I just decided, to learn usage of regular expressions with sed and awk (a little bit difficult at first time).
With perl can I use regex and the usage will be easier?
sed/grep/awk/perl are all different, they have different regexps so not easy...
But in general PCRE is a standard, or at least it is used as a reference implementation. And it is relatively complex.
Knowing what available in perl will make usage of regexp easier, but need to learn a lot.
(From the other hand perl is not only PCRE, but a programming language).
While the above regexes works, it feels brittle. You can of course tweak the regex to address corner cases, but that seems like extra work that increases complexity and does redundant work of handling a csv file.
You might expect these to work, but it will fail or have unexpected behavior:
Code:
'300, Thomas Jefferson, 1022, 1230 ' --> No Match
'40, Franklin D. Roosevelt, 1022, 1230 ' --> 40, Franklin D
' 30, Thomas Jefferson, 1022, 1230 ' --> No Match
The below uses the csv module to handle the loading of each row, takes the second column (the name) and splits it into a list delimited by spaces, then reverses that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.