Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
08-29-2017, 05:03 AM
|
#1
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Rep: 
|
change the order of first name, last name with sed
I have a file input.txt:
Quote:
1, George Washington, 1984, 1345
2, John Adams, 1234, 4321,
3, Thomas Jefferson, 1022, 1230
|
I need this output, only:
1, Washington George,
2, Adams John,
3, Jefferson Thomas,
Tried this command, but not working:
Quote:
sed -E 's/^([0-9]{1,2}), ([\w ]+) ([\w ]+),/\1 \3\2/g' input.txt
|
What is the main problem with my command?
Could you help, please?
I try to learn using sed, so I am newbie.
|
|
|
08-29-2017, 05:12 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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:
Code:
sed -E -e 's/^([0-9]{1,2}), ([[:alpha:] ]+) ([[:alpha:] ]+),/\1 \3 \2,/;' input.txt
The /g modifier is not needed since you will only do the substitution once.
|
|
1 members found this post helpful.
|
08-29-2017, 05:18 AM
|
#3
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,443
|
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.
|
|
1 members found this post helpful.
|
08-29-2017, 05:20 AM
|
#4
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Original Poster
Rep: 
|
Thank you for your help. I didn't know that.
It works.
|
|
|
08-29-2017, 05:40 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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:
Code:
perl -p -e 's/^([0-9]{1,2}), (\w+) +(\w+),/$1, $3 $2,/' input.txt
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" 
|
|
1 members found this post helpful.
|
08-29-2017, 05:54 AM
|
#6
|
Member
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69
Original Poster
Rep: 
|
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?
|
|
|
08-29-2017, 06:02 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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.
|
|
1 members found this post helpful.
|
08-29-2017, 06:04 AM
|
#8
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,443
|
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 ... 
|
|
1 members found this post helpful.
|
08-29-2017, 06:08 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,670
|
Quote:
Originally Posted by freeroute
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).
|
|
1 members found this post helpful.
|
08-29-2017, 07:50 PM
|
#10
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
To me, (within python,) this did not resemble a problem needing regex.
Quote:
^([0-9]{1,2}), ([[:alpha:] ]+) ([[:alpha:] ]+)
^([0-9]{1,2}), (\w+) +(\w+)
|
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.
Jupyter-Notebook demo of code: https://gist.github.com/anonymous/5c...d24ffee7cbe9a6
Code:
#!/usr/bin/env python3
import csv
with open('input.txt') as _f:
reader = csv.reader(_f)
for row in reader:
reversed_names = reversed(row[1].split())
reversed_names = ' '.join(reversed_names)
msg = '{id}, {rev_names},'.format(
id=row[0].strip(),
rev_names=reversed_names,
)
print(msg)
Code:
1, Washington George,
2, Adams John,
3, Jefferson Thomas,
300, Jefferson Thomas,
40, Roosevelt D. Franklin,
30, Jefferson Thomas,
|
|
|
08-29-2017, 08:06 PM
|
#11
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,443
|
The OP wanted to learn sed, not python. IMHO awk would be a better fit in this case, but that wasn't what was asked for.
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 01:01 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|