LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-16-2013, 08:37 PM   #1
petekat03
LQ Newbie
 
Registered: Dec 2010
Posts: 10

Rep: Reputation: 1
Rearrange Text with regex


Hi,

I'm trying to rearrange text using regex from;
01AA XXX YYYY ZZZ
02AA GGGG KKK
03AA WW UUUUUU FFF RRRR

to;
AA - 01 - XXX YYYY ZZZ
AA - 02 - GGGG KKK
AA - 03 - WW UUUUUU FFF RRRR

I'm using ^([0-9][0-9])(.+)\s(.+)

but, when rearranged with $2 - $1 - $3
I'm getting;
AA XXX - 01 - YYYY ZZZ
AA GGGG - 02 - KKK
AA WW - 03 - UUUUUU FFF RRRR

Thanks for any help..
Pete

Last edited by petekat03; 08-16-2013 at 08:41 PM.
 
Old 08-16-2013, 09:01 PM   #2
dob12460
Red Hat Content Author
 
Registered: Nov 2007
Location: Brisbane, Australia
Distribution: RHEL, Fedora
Posts: 29

Rep: Reputation: 4
sed -i -e 's/\(^[0-9][0-9]\)\([A-Z][A-Z]\)\(.*\)/\2 \- \1 \-\3/' <filename> seems to work.

Not sure that it's the most elegant way, but oh well

David
 
1 members found this post helpful.
Old 08-17-2013, 05:29 AM   #3
petekat03
LQ Newbie
 
Registered: Dec 2010
Posts: 10

Original Poster
Rep: Reputation: 1
Hi David,

Perfect!!

I just tweaked it slightly to suit ^([0-9][0-9])([A-Z][A-Z])(.*)

Thanks for your help,

Pete
 
Old 08-17-2013, 08:56 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or even:
Code:
sed -r 's/([0-9]{2})([^ ]*)/\2 - \1 -/' file
 
  


Reply

Tags
regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] regex text ted_chou12 Programming 6 03-11-2012 08:55 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
how to rearrange a text file with awk keenboy Programming 9 11-04-2009 08:07 AM
bash : crop some text with regex dodoecchi Programming 2 01-30-2009 02:10 PM
Can you parse text with regex? raypen Slackware 4 03-21-2006 01:26 PM


All times are GMT -5. The time now is 02:18 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration