Replacing string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing string
# 8  
Old 10-23-2008
sed "s/STRING_ZERO/$(eval echo \$VALUE$i)/g" FILE_ZERO.txt >> FILE_NEW.txt
# 9  
Old 10-23-2008
MySQL

Thanks a lot!!!
Now it works!Smilie

I have another question (the last one for today) I was looking for conditional compilation in bash script, something like #if... #endif in C language, but I couldn't find any information ...
Any suggestions???
# 10  
Old 10-23-2008
The problem in your previous post was this: the shell has to decide what is an argument and what are several arguments. Usually the space character (or any other whitespace for that matter) is used for that:

mv filea fileb

is interpreted as to take a file "filea" and rename (mv) it to "fileb". But lets suppose you have a file with whitespace in its name, how would you make the shell aware that

mv partone parttwo new name

should mean to take a file called partone parttwo and rename it to new name? How can the shell decide that you are not talking about 4 filenames, each separated by a blank or a file named partone parttwo new which should be renamed to name, etc.?

This is done by enclosing strings into double quotes. This groups the strings and make the shell aware where the boundaries are, even when the expression would be ambigous otherwise:

mv "partone parttwo" "new name"

This version removes all ambiguity.

In light of this lets consider your first line:

Code:
VALUES="1000 2000 3000"
for VAL in "${VALUES}"; do

The first thing the shell does is to replace the "${....}" with the content of the variable, so you commandline reads, after this:

Code:
VALUES="1000 2000 3000"
for VAL in "1000 2000 3000"; do

And now you should see the problem: you present a SINGLE element to "for" and therefore get a single pass of your loop. Removing the double quotes would do the trick:

Code:
VALUES="1000 2000 3000"
for VAL in ${VALUES}; do

This of course would mean that your values should not contain any whitespace, because the shell would split them and interpret them as two distinct values. To overcome this you should use an array and a while-loop:

Code:
array[0]="1000"
array[1]="2000"
array[2]="3000"
array[3]="4000"
array[4]="5000"
array[5]="ab cd"  # notice the whitespace
array[6]="6000"
iCnt=0

while [ $iCnt -lt ${#array[@]} ] ; do
     echo "array element number $iCnt is: \"${array[$iCnt]}\""
     (( iCnt += 1 ))
done

Regarding the "conditional compilation": there is no such thing, because bash is an interpreted language. You could write any garbage into the program code, as long as there is no attempt to execute it would never show:

Code:
if [ 1 = 0 ] ; then
     this is never going to be executed
     and it does not matter that this makes no sense at all as a
     bash-script
else
     echo "would you exchange the if- and else-branches"
     echo "you would get several errors"
fi

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a string

Hi All, I have a many folders in a directory under which there are many subdirectories containing text files containing the word "shyam" in them.I want all the files in all the directories containing "shyam to "ram" ?? sed "s/shyam/ram/g" does it ??But anyone can help me with the script ?? ... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Shell Programming and Scripting

Help with replacing string

Hi All, I have below requirement: I need to read each line in file.txt and replace string starting from position 9 to 24 {111111111111111,222222222222222,333333333333333} by common string "444444444444444" and save file. File.txt: 03000003111111111111111 ... (3 Replies)
Discussion started by: smalode
3 Replies

3. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

4. Shell Programming and Scripting

Help replacing string

Help! I'm trying this command but keep getting illegal syntax etc. awk '{ sub(/00012345/,"000123456"); print}' >newfile I don't understand. It works on one unix machine but not another! (4 Replies)
Discussion started by: Grueben
4 Replies

5. Shell Programming and Scripting

Replacing a string with a space

I'm trying to replace a string "99999999'" with the blank where ever is there in the file. Could you please help in unix scripting. Thank You. (6 Replies)
Discussion started by: vsairam
6 Replies

6. Shell Programming and Scripting

Replacing a string with new line

Hi, Can anyone help me know how to replace a string with the new line for ex: file1 val1 or val2 or val3 or I need to replace the "or" with new line. Thanks in advance (2 Replies)
Discussion started by: mr_manii
2 Replies

7. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

8. Shell Programming and Scripting

Need help regarding replacing a part of string

Hi all suppose i have a string "abacus sabre", i need to replace occurences 'ab' with 'cd' and i need to store this result into same string and i need to return this result from script to the calling function, where as the string is passed from calling function. i tried like this ... (1 Reply)
Discussion started by: veerapureddy
1 Replies

9. Shell Programming and Scripting

Problem replacing the string

I have three files that the string inside it I want to replace so my code will be #!/bin/bash read -p "please input the old string:" string1 read -p "please input the new string:" string2 sed -i "s/string1/string2/g" *.c but the problem is.. the string that I want to replace can't be... (2 Replies)
Discussion started by: Viken
2 Replies

10. Shell Programming and Scripting

string replacing

hii, i need a unix command which replaces all occurrences of a substring within a string with another substring. My solution: string="plalstalplal" sub1="al" sub2="mlkl" echo sed 's/$s2/$s3/g' < s1 > p i want to know how to read the variables s2 and s3.. thaks a lot bye (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question