LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-29-2018, 07:28 AM   #1
Dracaena
LQ Newbie
 
Registered: May 2018
Posts: 6

Rep: Reputation: Disabled
Merge first 2 columns


Hi all,

I am stuck on a large file in which I would like to merge the first 2 columns and then save it as a new file.

example:

JTK 1 duck cat snake
JTK 2 pig cow owl
JTK 3 fox wolf chicken
JTK1 1 eagle godwit curlew
JTK1 2 bustard mouse lion

would then be:

JTK1 duck cat snake
JTK2 pig cow owl
JTK3 fox wolf chicken
JTK11 eagle godwit curlew
JTK12 bustard mouse lion

I have tried

Code:
awk -F, '{print $1$2"\t"$3"\t"$4"\t"$5}' file.txt
but it just rendered the same file.

Many thanks
 
Old 05-29-2018, 07:33 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

I think it has to do with the field delimited, but cannot test that on a system.
 
Old 05-29-2018, 07:34 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Why are you using "-F," ?.
 
1 members found this post helpful.
Old 05-29-2018, 07:38 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Yes, take out the Input Field Separator.

Code:
awk '{ print $1$2, 3, $4, $5; }' OFS='\t'
If you have more fields or a variable number of fields then you will need a slight modification to that with a for loop.
 
1 members found this post helpful.
Old 05-29-2018, 07:56 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
If the number of fields is not always 5, it gets a little more complicated. Also, that awk will change all spaces (1 or many tabs/spaces) into one tab.

Another way to look at it, is to delete the first space in every line of the file. If that's really all that's needed, you could use sed:

Code:
sed 's/ //' file.txt
 
Old 05-29-2018, 08:06 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Column based data is generally better handled in awk - it even provides NF so you can loop over a (possibly indeterminate) number of fields.
If sed is to be used, better to use character classes to be safe.
 
Old 05-29-2018, 08:50 AM   #7
Dracaena
LQ Newbie
 
Registered: May 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the help and suggestions!
 
Old 05-30-2018, 05:15 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
...

Last edited by AnanthaP; 05-31-2018 at 02:35 AM. Reason: Wrongly posted it again. So this is erased. (Unable to delete)
 
Old 05-30-2018, 05:25 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Code:
awk '{
       n =split($0,a," "); 
       for(i=1;i<=n;i++) {
         printf("%s",a[i]) ;
         if( (i>1) && (i<n)) printf(" ") ;
       }
       printf("\n") ;
}'
OK

Last edited by AnanthaP; 05-30-2018 at 05:43 AM.
 
  


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
[SOLVED] How to merge lines based on number of columns shriankur Linux - Newbie 14 03-12-2018 01:34 AM
merge columns from multiple files in a directory based on match of two columns prasanthi yanamala Linux - Newbie 2 11-12-2015 10:11 AM
merge columns from multiple files vijay_babu1981 Linux - Newbie 21 06-24-2014 06:59 AM
how to merge multiple columns into one column linuxon Linux - Newbie 6 03-14-2012 11:17 AM
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 10:52 AM

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

All times are GMT -5. The time now is 11:37 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