Array inside sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Array inside sed
# 1  
Old 01-29-2015
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.
HTML Code:
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-

Code:
 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-
Code:
-bash: syntax error near unexpected token `counter=$(($counter+1))'

Thanks a lot!

Last edited by saleheen; 01-29-2015 at 01:56 PM.. Reason: mistake in code
# 2  
Old 01-29-2015
Hello Saleheen,

Following code may help you in same, but not tested though.
Kindly try the same and let me know if this helps.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-29-2015
Hi Ravinder

Thanks a lot! But I think it didn't work Smilie For 5 files,the output looks like this-
PHP Code:
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
PHP Code:
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
# 4  
Old 01-29-2015
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
Code:
awk 'NR==1 {A=$2} NR%2 {$2=A++} 1' OFS="\t" file[1-5]

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-29-2015
Thanks a lot man! Fantastic! It worked! Could you kindly explain the code? I'm ignorant Smilie And also I just wanted how I can change array elements using sed within a loop. May be like this-
Code:
 
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!

Last edited by saleheen; 01-29-2015 at 02:23 PM.. Reason: Just adding some lines
# 6  
Old 01-29-2015
Code:
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.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-30-2015
Thanks a lot man!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. Programming

Array initialization inside class in C++

const int VALUES = {7,4,2,1,0}; //or int VALUES = {7,4,2,1,0};this statement inside a class definition gives error. Why? (3 Replies)
Discussion started by: milhan
3 Replies

3. Shell Programming and Scripting

How to use variable inside array?

I tried to use variable inside an array variable, but its not working as expected.:wall: ENV1=123 ENV1=789 ENV1=120 ENV2=567 if then name=ENV1 echo "${name}" echo "${name}" echo "${name}" else name=ENV1 echo "${name}" fi Output: ./val.sh 1 123 (2 Replies)
Discussion started by: Jayavinoth
2 Replies

4. Shell Programming and Scripting

unique inside array

I have a file root@server # cat /root/list12 11.22.33.44 22.33.44.55 33.44.55.66 33.44.55.66 33.44.55.66 I try to pass to array and display unique. root@server# cat /root/test12.sh #!/bin/bash #delcare array badips and accumulate values to array elemenrs badips=( $( cat... (4 Replies)
Discussion started by: anil510
4 Replies

5. Shell Programming and Scripting

Calling array inside awk

Hello I have the file df.tmp FS is actually the / FS but escape character\ and end of line $ is used in order to fetch exctly / and not other filesystems. awk '/\/$/ {print $(NF-1)+0}' df.tmp will work properly and return a value eg. 60 but when I am trying to issue the command with the array... (3 Replies)
Discussion started by: drbiloukos
3 Replies

6. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

7. Shell Programming and Scripting

using array inside awk

Hi All, I have the following code sequence for reading some bulk file and moving the content to two different arrays. while read data do THREEG_PATTERN=`echo $data | egrep "3G"` if then NEW_THREEG_PATTERN=`echo $THREEG_PATTERN | cut -d " " -f2` ... (12 Replies)
Discussion started by: subin_bala
12 Replies

8. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. UNIX for Advanced & Expert Users

Array inside an array

hi All, I have a array as follows, array1=("xx" "abc" "def" "xyz") and each array1 is also storing some array values, like array1=abc and abc=("a" "b" "c") etcetera etcetra......... Note : each subarray under array1 have index 3 i.e. it can max contain 3 values if i echo ${abc} ... (5 Replies)
Discussion started by: manas_ranjan
5 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question