Copy the data column to adjacent column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy the data column to adjacent column
# 1  
Old 07-22-2010
Copy the data column to adjacent column

Hi,

i have file which contains only one record like below

Code:
a|b|c|....

The no of columns is not known. The expected output is as below:
Code:
a|a|b|b|c|c|...

Please provide me your suggestions.

OS: unix

Thanks,
Selva

Last edited by vgersh99; 07-22-2010 at 05:04 PM.. Reason: code tags, please!
# 2  
Old 07-22-2010
Code:
 echo 'a|b|c|' | sed 's/[^|][^|]*/&|&/g'

These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-22-2010
can you try this Smilie
Code:
# sed 's/[a-z]/&|&/g' infile
a|a|b|b|c|c|....

# 4  
Old 07-22-2010
Thanks for your reply. The code is working for a|b|c. But it is not working for 1 W/E 01/06/07|1 W/E 01/05/08

Thanks

---------- Post updated at 03:12 PM ---------- Previous update was at 03:08 PM ----------

vgersh99's code is working fine. Could you please explain me the code?

---------- Post updated at 03:27 PM ---------- Previous update was at 03:12 PM ----------

Hi,

I would like to add some content to the column as like below.

input:
Code:
a|b|c

output:
Code:
a(str_cnt)|a(Tot_cnt)|b(str_cnt)|b(Tot_cnt)|c(str_cnt)|c(Tot_cnt)

How can i get that?

Thanks,
Selva

Last edited by vgersh99; 07-22-2010 at 05:34 PM.. Reason: code tags, please!
# 5  
Old 07-22-2010
Code:
echo 'a|b|c|' | sed 's#[^|][^|]*#&(str_cnt)|&(Tot_cnt)#g'

# 6  
Old 07-22-2010
Excellent vgersh99. I really appreciate your help.

Thanks a lot.
Selva
# 7  
Old 07-22-2010
Code:
awk  'BEGIN{FS=OFS="|"} {for (i=1;i<=NF;i++) $i=$i FS $i}1' urfile

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

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 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

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. Shell Programming and Scripting

Copy column string and put in different column

Hello Here is my input: SU3902 SU3902A NS29C (10) (00) Q1J1 0 SU3902 SU3902B VLR05 (20) (02) Q2H1 4 SU3902 SU3902C NBR22 (30) (06) Q3R5 8 SU3904 SU39047 NSV19 (11) (09) Q4k6 2 SU3904 SU39048 LB231 (12) (05) Q5k1 6 SU3904 SU39049 11VLT (13) (08) Q10C1 10 SU3904 SU3904A 25R05 (15) (06)... (3 Replies)
Discussion started by: pareshkp
3 Replies

8. Shell Programming and Scripting

copy data from different file with some same column

I have file1 have below data ABCDE2012 ABCDE2012120 -099.8 -099.4 00.4 ABCDE2012 ABCDE2012124 -104.6 -103.6 01.0 ABCDE2012 ABCDE2012128 -104.4 -103.1 01.4 file2 ABCDE2012 ABCDE2012120 15.4 ABCDE2012 ABCDE2012124 18.5 ABCDE2012 ABCDE2012128 20.1 ABCDE2012 ABCDE2012122 0.5 ... (1 Reply)
Discussion started by: pareshkp
1 Replies

9. Shell Programming and Scripting

Adjacent row and column check in Perl

HI, i am new to perl world. And i am trying to compress a file, as given below procedure. INPUT FILE: 1 1 2 1 ==> R1 2 1 3 1 ==> R2 3 1 4 1 ==> R3 OUTPUT FILE: 1 1 4 1 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

10. Shell Programming and Scripting

How to subtract the adjacent lines from a single column?

Hi All, I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column. For Example: (Input) Col1 5 10 12 6 9 12 5 . . . .output should be like this: (12 Replies)
Discussion started by: Fredrick
12 Replies
Login or Register to Ask a Question