![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
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].) |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Why not?
Code:
awk -F, '$7=$2' input > output |
|
#5
|
|||
|
|||
|
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 Code:
sed 's/^([^,]*,([^,]*,)([^,]*,){4})[^,]*,/\1\2/' file
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|