sed and arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed and arrays
# 1  
Old 09-29-2015
sed and arrays

im trying to get sed to replace values while stepping through and array;-


Code:
package=( P1 PA PXL PKL )
tier=( T1 T2 T3 T4 )

for p in ${package[@]}
      do
      sed s/$p/${tier[@]}/ file.txt
done

im just getting command garbled

sed: command garbled: s/P1/T1
sed: command garbled: s/PA/T1
sed: command garbled: s/PXL/T1
sed: command garbled: s/PKL/T1

what am i doing wrong?
# 2  
Old 09-29-2015
That because of the spaces between the "tier" elements; sed considers the "s" command unterminated there.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-29-2015
Quote:
Originally Posted by RudiC
That because of the spaces between the "tier" elements; sed considers the "s" command unterminated there.
ok that fixed that issue but it now means all the elements in that array have been replaced so the file before:-

Code:
HELLO-P1-B
YO-PA-B
HI-P1Q-B
MWM-PXL-B
SFDS-PKL-B
SD-PAA-B
ASHJ-P1-B

now looks like

Code:
HELLO-T1T2T3T4-B
YO-PA-B
HI-T1T2T3T4Q-B
MWM-PXL-B
SFDS-PKL-B
SD-PAA-B
ASHJ-T1T2T3T4-B
HELLO-P1-B
YO-T1T2T3T4-B
HI-P1Q-B
MWM-PXL-B
SFDS-PKL-B
SD-T1T2T3T4A-B
ASHJ-P1-B
HELLO-P1-B
YO-PA-B
HI-P1Q-B
MWM-T1T2T3T4-B
SFDS-PKL-B
SD-PAA-B
ASHJ-P1-B
HELLO-P1-B
YO-PA-B
HI-P1Q-B
MWM-PXL-B
SFDS-T1T2T3T4-B
SD-PAA-B
ASHJ-P1-B

rather than

Code:
HELLO-T1-B
YO-T2-B
HI-P1Q-B
MWM-T3-B
SFDS-T4-B
SD-PAA-B
ASHJ-T1-B

# 4  
Old 09-29-2015
That's not that easy. Even this
Code:
for p in ${!package[@]};       do       sed -ie "s/-${package[$p]}-/-${tier[$p]}-/" file; done

is fragile. sed may not be the approach of choice.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-29-2015
would awk be a better tool?
# 6  
Old 09-29-2015
Code:
awk '
NR==1   {split (package, P)
         split (tier, T)
        }
$2 in P {$2=T[$2]}
1
' FS="-" tier="T1 T2 T3 T4" package="P1 PA PXL PKL" file
HELLO-T1-B
YO-T2-B
HI-P1Q-B
MWM-T3-B
SFDS-T4-B
SD-PAA-B
ASHJ-T1-B

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Arrays

Am using bash For eg: Suppose i have a array arr=(1 2 3 4 5 6 7 8 9 10 11 12) suppose i give input 5 to a script and script should able to print values greater than or equal to 5 like below: Input: 5 output: 5,6,7,8,9,10,11,12 (7 Replies)
Discussion started by: manid
7 Replies

2. Shell Programming and Scripting

Using arrays?

I have never used arrays before but I have a script like this: var1=$(for i in $(cat /tmp/jobs.021013);do $LIST -job $i -all | perl -ne 'print /.*(\bInfo.bptm\(pid=\d{3,5}).*/' | tr -d "(Info=regpid" | tr -d ')'; $LIST -job $i -all | cut -f7 -d','| sed -e "s/^\(*\)\(*\)\(*\)\(.*\)/\1... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Programming

Arrays in C++

I've noticed something interesting in C++ programming. I've always done tricky stuff with pointers and references to have functions deal with arrays. Doing exercises again out of a C++ book has shown me an easier way, I didn't even know was there. It's weird to me. When dealing with arrays, it... (4 Replies)
Discussion started by: John Tate
4 Replies

4. Shell Programming and Scripting

How can I use the arrays ?

Hi all, I have a file test1.txt with the below contents abc def ghj xyz I tried printing these values using arrays. Script tried : =========== set -A array1 `cat test1.txt` count=${#array1 } i=0 while do echo "element of array $array1" done (1 Reply)
Discussion started by: dnam9917
1 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. UNIX for Dummies Questions & Answers

arrays how to?

Hello, I am some what of a newbie to awk scripting and I seem to be struggling with this problem. I know I need to use arrays but I can't figure out how to use them. I have an input file that looks like this; Name,Team,First Test, Second Test, Third Test Crystal,Red,5,17,22... (1 Reply)
Discussion started by: vlopez
1 Replies

7. Shell Programming and Scripting

arrays in awk

Hi, I have the following data in a file for example: Name="Fred","Bob","Peterson","Susan","Weseley" Age="24","30","28","23","45" Study="English","Engineering","Physics","Maths","Psychology" Code="0","0","1","1","0" Name="Fred2","Bob2","Peterson2","Susan2","Weseley2"... (14 Replies)
Discussion started by: james2009
14 Replies

8. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

9. Shell Programming and Scripting

arrays in C shell

hi guys, i have the following code in C shell.. set i=0 while ($i < 11) master_array=${ARRAY} i++ done it gives me error at line 3: Variable syntax. what is wrong here? any help is appreciated. (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. Shell Programming and Scripting

Arrays

Dear all, How can i unset arrays. I mean all the subscripts including the array after using them. Could you direct me to some links of array memory handling in the korn shell. Thanks (2 Replies)
Discussion started by: earlysame55
2 Replies
Login or Register to Ask a Question