The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting/copy a column into a new column folashandy UNIX for Advanced & Expert Users 1 02-21-2008 10:24 AM
Automatic Copy of File Contents to Clipboard ilak1008 Shell Programming and Scripting 5 10-10-2006 08:43 AM
Copy selected contents from file misenkiser Shell Programming and Scripting 4 10-05-2006 08:12 AM
copy folder contents robsk8_99 UNIX for Dummies Questions & Answers 1 02-22-2005 03:54 PM
How to Copy Contents from CD to Hard disk vr76413 UNIX for Dummies Questions & Answers 4 07-05-2003 09:20 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-12-2008
Registered User
 

Join Date: Aug 2007
Posts: 41
copy contents of one column in another

Hi,

I want to pick contents of a column in a file and copy the contents of this to other column.

awk can be used for this, but the number of fields are higher so awk will not help. Any other way to do this.

e.g following file has some contents a follows

Code:
a,b,c,d,e,f,9,0
i need to copy contents of second column(b here) to 7th field(9 here).
Reply With Quote
Forum Sponsor
  #2  
Old 05-12-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Do you want to overwrite the seventh field, or add a field?

Code:
perl -laF, -ne 'splice(@F,6,0,$F[1]); print join (",", @F)' file
This is the moral equivalent of "cut -f1-6,2,7-" except regrettably cut doesn't work that way. I used to have a "perl cut" called pcut in my ~/bin but it didn't get used all that much.

If you want to replace the seventh field instead, the third element in the splice controls that; change it to a 1 to replace instead of add. (Array indices in Perl are zero-based, that's why you see 6 and $F[1] instead of 7 and $F[2].)
Reply With Quote
  #3  
Old 05-12-2008
Registered User
 

Join Date: Aug 2007
Posts: 41
Thanks for the response but another issue here is that perl is not there

Do we have some solution using sed or some other way?

I just need to replace. No need to retain the value for 7th field.
Reply With Quote
  #4  
Old 05-12-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 736
Why not?
Code:
awk -F, '$7=$2' input > output
Reply With Quote
  #5  
Old 05-12-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Your comment that the field numbers in awk are insufficient lead me to suspect that this is not going to be very sustainable, but for what it's worth, here's a sed solution.

Code:
sed 's/^\([^,]*,\([^,]*,\)[^,]*,[^,]*,[^,]*,[^,]*,\)[^,]*,/\1\2/' file
There are some variations in sed syntax; if you're lucky, you could even get one which understands

Code:
sed 's/^([^,]*,([^,]*,)([^,]*,){4})[^,]*,/\1\2/' file
without those pesky backslashes, and with the neat {4} repetition count.

I'd still concur that you probably want to use awk for this if it's a real problem and the field numbers are larger than half a dozen. Probably you can work around this issue even if your awk is ... old and grumpy.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0