LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-29-2017, 05:03 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
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.
 
Old 08-29-2017, 05:12 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
Old 08-29-2017, 05:18 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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.
Old 08-29-2017, 05:20 AM   #4
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thank you for your help. I didn't know that.
It works.
 
Old 08-29-2017, 05:40 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
Old 08-29-2017, 05:54 AM   #6
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
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?
 
Old 08-29-2017, 06:02 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
Old 08-29-2017, 06:04 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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.
Old 08-29-2017, 06:08 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
Quote:
Originally Posted by freeroute View Post
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.
Old 08-29-2017, 07:50 PM   #10
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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,
 
Old 08-29-2017, 08:06 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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.
Old 08-31-2017, 11:39 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For sed you could read http://www.grymoire.com/Unix/Sed.html, http://sed.sourceforge.net/ .
For regexes & the different regex engines for the main tools (inc Perl), THE book is http://regex.info/book.html
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is the sed-Order not saving the changes in the .dat file? Bobbic Linux - Newbie 4 11-27-2015 10:15 AM
wrong submission in a for loop by usind the sed order Bobbic Linux - Newbie 3 11-22-2015 12:00 PM
Reverse the order of lines using sed Karljoe2 Programming 4 12-26-2012 10:38 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration