Copying first column to be second column | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Copying first column to be second column

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-04-2012
narachaid narachaid is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 28 October 2013, 1:09 PM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Copying first column to be second column

Hi guys,

Does anyone know how to copy the first column and paste it as the second column in a file?

For example:

Input:

Code:
A  1  3
B  3  7
C  5  2
D  9  4

Output:

Code:
A  A  1  3
B  B  3  7
C  C  5  2
D  D  9  4

Thank you very much!!

Last edited by Franklin52; 12-05-2012 at 05:11 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 12-04-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 27 November 2014, 6:01 PM EST
Posts: 3,720
Thanks: 7
Thanked 1,147 Times in 1,118 Posts
Try:
Code:
awk '{$1=$1" "$1}1' file

Sponsored Links
    #3  
Old 12-04-2012
narachaid narachaid is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 28 October 2013, 1:09 PM EDT
Posts: 21
Thanks: 9
Thanked 0 Times in 0 Posts
Thank you very much!!
    #4  
Old 12-04-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 14 November 2014, 7:56 AM EST
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
$ awk '$2=$1" "$2' a.txt
A A 1 3
B B 3 7
C C 5 2
D D 9 4

Sponsored Links
    #5  
Old 12-04-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try also:

Code:
sed 's/\([^ ]*\).*/\1 &/' input


Code:
awk '{print $1,$0}' input

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename a header column by adding another column entry to the header column name Vavad UNIX for Dummies Questions & Answers 1 08-06-2011 02:02 PM
Rename a header column by adding another column entry to the header column name URGENT!! Vavad Shell Programming and Scripting 4 08-05-2011 01:35 PM
Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 rydz00 Shell Programming and Scripting 7 11-09-2010 11:28 AM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 06:27 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 10:57 AM



All times are GMT -4. The time now is 05:41 AM.