copy contents of one column in another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy contents of one column in another
# 1  
Old 05-12-2008
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).
# 2  
Old 05-12-2008
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].)
# 3  
Old 05-12-2008
Thanks for the response but another issue here is that perl is not there Smilie

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  
Old 05-12-2008
Why not?
Code:
awk -F, '$7=$2' input > output

# 5  
Old 05-12-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Unable to copy contents

I am trying to move content of a folder usingls /backup/db_backups/INCREMENTAL/|while read file ; do mv $file /backup_LOCAL/db_backups/INCREMENTAL/ done...but when I run this command using a bash script I'm getting this error ./test.sh mv: cannot stat... (6 Replies)
Discussion started by: rocking77
6 Replies

3. Shell Programming and Scripting

Copy contents of one file to another

I need to write a script (in bash) that copies the content of the first file in each folder of a directory to the second file in the same folder. I tried this and it didn't work - it just came back with errors and I'm not sure how to fix it. Help is very much appreciated! for mpdir in... (4 Replies)
Discussion started by: LeftoverStew
4 Replies

4. UNIX for Advanced & Expert Users

Copy a column to another column in UNIX fixedwidth file

Hi All, I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file. Please let me know how can i achive using a single command in fixed width file. Also source column length is 4 chars and target column length... (4 Replies)
Discussion started by: kiranparsha
4 Replies

5. Shell Programming and Scripting

Copy contents of whatever's loaded into the CD drive

Hello everyone, I have about 1500 compact discs of seismic data that I need to retrieve and place onto the hard drive so that I can index and process them. The data was generated at 20 seismic stations and each disc has a been assigned unique name. The name is NOT necessarily what is on the... (7 Replies)
Discussion started by: ws6transam
7 Replies

6. Shell Programming and Scripting

Need help to copy contents of a file

Hi, I am stuck up with a problem of copying the contents of a directory where one of the folder name is changed daily. Problem: I have the folder structure as: RefWorlds2/LINGCC4_X64/odsdev/odessy/UTI/621GA_build_xxx/.../.. In the above path the build number (xxx) will be changed... (3 Replies)
Discussion started by: SathaKarni
3 Replies

7. Shell Programming and Scripting

copy the contents between two keywords to a new file.

Hi All, I want to edit my gate level netlists by searching for the content between two patterns eg: ff1 \test/a0 ( .CLK(\test/ClkInt0_acb_00x1 ),.D(\test/Rakicc ), .QB(\test/X ), .VDD(1'b1), .VSS(1'b0)); ff1 \test/a1 ( .CLK(\test/medis0_acb_00x1 ),.D(\test/hedwc ), .QB(\test/X ),... (6 Replies)
Discussion started by: naveen@
6 Replies

8. Shell Programming and Scripting

Copy selected contents from file

I want to capture contents of a file between 2 strings into another file for eg all lines in between the keywords "start log" and "end log" should be copied into another file (4 Replies)
Discussion started by: misenkiser
4 Replies

9. UNIX for Dummies Questions & Answers

copy folder contents

I need to make a new dir in side the dir lab5 the new dir is called testLab5 without changing directories copy all files from your lab5 directory into your testLab5 directory then i have to without chaning directories and using exactly one command remove all files that start with the... (1 Reply)
Discussion started by: robsk8_99
1 Replies

10. UNIX for Dummies Questions & Answers

How to Copy Contents from CD to Hard disk

Hello all.. Iam New to Unix Environment. I need to copy .cpio file from CD to a Folder on Sun 5.8 Box. Can anyone give me the commands to execute this ?.. Thanks in advance Ron (4 Replies)
Discussion started by: vr76413
4 Replies
Login or Register to Ask a Question