Array inside sed

Hi guys

Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one.

Ow            1230   16.000000   -0.834000   16.083957                  
     1.751652398        -17.20094528        -4.450623277                
Hw            1231    1.000000    0.417000   14.341378                  
     2.128451025        -16.83399008        -5.250372494                
Hw            1232    1.000000    0.417000   16.844104                  
    0.9093140466        -16.75614395        -4.356548788                
Ow            1233   16.000000   -0.834000   20.225603                  
    -2.889503614        -23.61890070         10.03599967                
Hw            1234    1.000000    0.417000   20.855176                  
    -2.102487366        -23.08165809         10.12659865                
Hw            1235    1.000000    0.417000   20.448925                  
    -2.604411345         24.18827587         10.26811687                
Ow            1236   16.000000   -0.834000    9.718498                  
    -18.13116183         15.29853776        -10.10585481                
Hw            1237    1.000000    0.417000    9.855829                  
    -18.86525637         14.71715826        -10.30418717                
Hw            1238    1.000000    0.417000    9.294911                  
    -17.66286350         15.38507405        -10.93618054                
Ow            1239   16.000000   -0.834000   10.477756                  
     11.91804137        -21.91087964        -11.56566494   

I want to merge all the 100 files. The second field of the 1st,3rd..so on lines shows the atom number i.e 1230,1231. All the files are numbered from 1230..1239. So for the 1st file this numbers are ok, for the second file numbering should start from 1240,1241..and this should go on all the way through the last file. I did merge the files through a loop, The way I was trying to get those atom numbers in an array, replace those array elements with a counter. My thinking was this-

 myarr=($(awk 'NR % 2 ==1' TestMe | awk '{print $2}'))
echo ${myarr[@]} 
counter=1230; 
for i in "${myarr[@]}" 
do sed "s/$i/$j/g" TestMe;
counter=$(($counter+1)); 
done

I just don't know how to replace array elements in a sed command. Or is it the right way to loop through array elements. And whenever I tried to use that piece of code it said-

-bash: syntax error near unexpected token `counter=$(($counter+1))'

Thanks a lot!

Hello Saleheen,

Following code may help you in same, but not tested though.
Kindly try the same and let me know if this helps.

awk 'FILENAME=="file1" {if(NR%2!=0){print $1 OFS $2;A=$2}} FILENAME!="file1"{if(NR%2!=0){print $1 OFS ++A}}' file*

Assuming you are all 100 files have name from file1, file2, file3... file100 .

Thanks,
R. Singh

Hi Ravinder

Thanks a lot! But I think it didn't work :frowning: For 5 files,the output looks like this-

Ow 1230
Ow01230
Hw11231
Hw11231
Hw21232
Hw21232
Ow31233
Ow31233
Hw41234
Hw41234
Hw51235
Hw51235
Ow61236
Ow61236
Hw71237
Hw71237
Hw81238
Hw81238
Ow91239
Ow91239
Ow101239
Hw111239
Hw121239
Ow131239
Hw141239
Hw151239
Ow161239
Hw171239
Hw181239
Ow191239
Ow201239
Hw211239
Hw221239
Ow231239
Hw241239
Hw251239
Ow261239
Hw271239
Hw281239
Ow291239
Ow301239
Hw311239
Hw321239
Ow331239
Hw341239
Hw351239
Ow361239
Hw371239
Hw381239
Ow391239
Ow401239
Hw411239
Hw421239
Ow431239
Hw441239
Hw451239
Ow461239
Hw471239
Hw481239
Ow491239

One thing is the output is wrongly numbered but also it's disordered. In my file I had always

Ow
Hw
Hw
Ow
Hw
Hw
...

but in the output I have 2 consecutive Ow's. I have to admit I don't understand your code. Could you kindly explain it to me?
Thanks a lot! Really appreciate it!

Saleheen

Keeping your line format for the modified lines is not easy. I use <TAB> as the output field separator. And, no precaution is taken to get the input files in the correct order. Try

awk 'NR==1 {A=$2} NR%2 {$2=A++} 1' OFS="\t" file[1-5]

Thanks a lot man! Fantastic! It worked! Could you kindly explain the code? I'm ignorant :frowning: And also I just wanted how I can change array elements using sed within a loop. May be like this-

 
myarr=($(awk 'NR % 2 ==1' TestMe | awk '{print $2}'))
echo ${myarr[@]} 
counter=1230; 
len=${#myarr[*]}
for i in {1..len} 
do sed "s/${myarr[@]} /$counter/g" TestMe;
counter=$(($counter+1)); 
done

Thanks a lot! Really appreciate it!

awk 'NR==1 {A=$2}          # initialize the A variable from $2 on the first line
     NR%2  {$2=A++}        # on every other line, replace $2 by the post-incremented A value
     1                     # = TRUE, do default action (print)
    ' OFS="\t" file[1-5]   # set the output field separator, let the shell expand the files 1 ... 5 

---------- Post updated at 19:33 ---------- Previous update was at 19:29 ----------

Without a detaild analysis of your shell script: be careful to use it as is, as sed will replace every occurrence of myarr[x] in TestMe, very probably leading to undesired results. And, don't use myarr[@] in that loop.

Thanks a lot man!