Copy the data column to adjacent column

Hi,

i have file which contains only one record like below

a|b|c|....

The no of columns is not known. The expected output is as below:

a|a|b|b|c|c|...

Please provide me your suggestions.

OS: unix

Thanks,
Selva

 echo 'a|b|c|' | sed 's/[^|][^|]*/&|&/g'
2 Likes

can you try this :slight_smile:

# sed 's/[a-z]/&|&/g' infile
a|a|b|b|c|c|....

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:

a|b|c

output:

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

echo 'a|b|c|' | sed 's#[^|][^|]*#&(str_cnt)|&(Tot_cnt)#g'

Excellent vgersh99. I really appreciate your help.

Thanks a lot.
Selva

awk  'BEGIN{FS=OFS="|"} {for (i=1;i<=NF;i++) $i=$i FS $i}1' urfile
 
# cat infile
1 W/E 01/06/07|1 W/E 01/05/08|1 W/E 01/05/09|1 W/E 01/05/10|1 W/E 01/05/11|1 W/E 01/05/12|1 W/E 01/05/13
# sed "s/[0-9] .\/. ..\/..\/..|\|$/&&/g;s/|\([0-9] .\/. ..\/..\/..\)$/|\1|\1/" infile
1 W/E 01/06/07|1 W/E 01/06/07|1 W/E 01/05/08|1 W/E 01/05/08|1 W/E 01/06/09|1 W/E 01/06/09|1 W/E 01/06/10|1 W/E 01/06/10|1 W/E 01/06/11|1 W/E 01/06/11|1 W/E 01/06/12|1 W/E 01/06/12|1 W/E 01/06/13|1 W/E 01/06/13