LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-08-2011, 08:01 AM   #1
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Rep: Reputation: 15
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.
Old 04-08-2011, 08:13 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,337

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
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.
 
Old 04-08-2011, 08:19 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
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.
Old 04-08-2011, 08:21 AM   #4
mad_penguin
Member
 
Registered: Mar 2008
Posts: 69

Original Poster
Rep: Reputation: 15
Thanks a lot mate!
 
Old 04-08-2011, 09:14 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Mark the thread as solved.

By the way, you can also do it with sed:

Code:
sed 's/[^ ]\+ [^ ]\+/\1.\2/'
 
Old 04-08-2011, 09:50 AM   #6
wpeckham
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

Rep: Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002Reputation: 3002
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.
 
Old 04-09-2011, 08:31 AM   #7
PMorph
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 213

Rep: Reputation: 31
Care to elaborate? colucix's solution is hardly too much of a trouble to implement.
 
Old 04-09-2011, 08:47 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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
 
Old 04-09-2011, 10:41 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ awk '$1=$1' OFS="." file
 
Old 04-09-2011, 10:42 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by wpeckham View Post
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.
 
Old 04-09-2011, 10:43 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by grail View Post
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
 
  


Reply


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] How to merge this awk and sed codes in a single one? cgcamal Programming 10 03-14-2011 12:59 AM
[SOLVED] awk command to merge two files silkysue Linux - Newbie 7 01-27-2011 10:14 AM
[SOLVED] merge 2 files with AWK by the field value dayamoon Linux - Newbie 8 06-03-2010 02:06 AM
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 10:51 AM
awk merge and sum lines problem lalo4080 Programming 4 08-12-2008 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:39 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
Open Source Consulting | Domain Registration