Programming This 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.
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.
|
 |
04-08-2011, 08:01 AM
|
#1
|
Member
Registered: Mar 2008
Posts: 69
Rep:
|
help on awk |merge two words
Heloo
I have a file that contains name like, firs_name last_name, on a row (
ex: John Smith
Michael Jones
Maria Lee
I need something to merge firs_name + last_name by putting a '.' (dot) between, like: John.Smith etc.
I think awk can do it but I don't know how.
Any help would be much appreciated.
Thanks
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
04-08-2011, 08:13 AM
|
#2
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,337
|
if there are always exactly two names separated by a space, awk is fine:
Code:
cat sample
John Smith
Michael Jones
Maria Lee
awk 'BEGIN {OFS="."} {print $1,$2}' < sample
John.Smith
Michael.Jones
Maria.Lee
This changes the output field separator to ".", then prints the first two fields of each input line to each output line. The output record separator (newline) is printed at the end of each line.
|
|
|
04-08-2011, 08:19 AM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
For any number of fields:
Code:
awk -v OFS=. '$1=$1'
the -v part is the same as the BEGIN section in the previous example. The $1=$1 expression forces awk to split the record into fields, so that the default action (print) actually uses the specified output field separator.
|
|
2 members found this post helpful.
|
04-08-2011, 08:21 AM
|
#4
|
Member
Registered: Mar 2008
Posts: 69
Original Poster
Rep:
|
Thanks a lot mate! 
|
|
|
04-08-2011, 09:14 AM
|
#5
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Mark the thread as solved.
By the way, you can also do it with sed:
Code:
sed 's/[^ ]\+ [^ ]\+/\1.\2/'
|
|
|
04-08-2011, 09:50 AM
|
#6
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,282
|
merge names
Actually, awk (while it will work) may not be the best tool for the job. Multiple solutions come to mind involving perl, tr, and simple bash scripting.
|
|
|
04-09-2011, 08:31 AM
|
#7
|
Member
Registered: Sep 2003
Distribution: Debian
Posts: 213
Rep:
|
Care to elaborate? colucix's solution is hardly too much of a trouble to implement.
|
|
|
04-09-2011, 08:47 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Quote:
Care to elaborate? colucix's solution is hardly too much of a trouble to implement.
|
I agree with PMorph. The only extra advantage you might have of using say sed is not having to use a temp file assuming you are making
alterations to the original:
Code:
sed -i 's/ /./' file
|
|
|
04-09-2011, 10:41 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
$ awk '$1=$1' OFS="." file
|
|
|
04-09-2011, 10:42 AM
|
#10
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by wpeckham
Actually, awk (while it will work) may not be the best tool for the job. Multiple solutions come to mind involving perl, tr, and simple bash scripting.
|
Nonsense. awk is there to reduce excessive bash scripting. Perl is over-rated when it comes to file parsing.
|
|
|
04-09-2011, 10:43 AM
|
#11
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by grail
I agree with PMorph. The only extra advantage you might have of using say sed is not having to use a temp file assuming you are making
alterations to the original:
Code:
sed -i 's/ /./' file
|
where is the extra advantage?
Code:
$ awk '{$1=$1; print > FILENAME}' OFS="." file
|
|
|
All times are GMT -5. The time now is 05:39 PM.
|
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
|
|