Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Copying first column to be second column

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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? Linux

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 Unix and Linux 12-04-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts
Try:
Code:
awk '{$1=$1" "$1}1' file

Sponsored Links
    #3  
Old Unix and Linux 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!! Linux
    #4  
Old Unix and Linux 12-04-2012
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 7 February 2016, 11:39 PM EST
Posts: 3,065
Thanks: 34
Thanked 662 Times in 641 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 Unix and Linux 12-04-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 August 2015, 4:23 PM EDT
Location: Houston, Texas, USA
Posts: 685
Thanks: 0
Thanked 207 Times in 199 Posts
try also:

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


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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:44 AM.