Replace a string based on input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string based on input
# 8  
Old 08-20-2019
Not sure (as your requirement is still unclear) if this one, using recent bash's extglob option and some "parameter expansions", gets you somewhere:


Code:
shopt -s extglob
rplst="nz|usa|uk"
for ctry in de nz usa uk aus aut nl
  do    VLD=${ctry/${ctry/@($rplst)}} 
        SD=${VLD:+diff}
        final_var=${SD:-source}_table_${VLD:-aus}_final
        echo $final_var
done
source_table_aus_final
diff_table_nz_final
diff_table_usa_final
diff_table_uk_final
source_table_aus_final
source_table_aus_final
source_table_aus_final


Last edited by RudiC; 08-20-2019 at 09:07 AM..
# 9  
Old 08-20-2019
Let me be clear once again as per below code, i am getting ctry_input as input to my shell program. Where in if i get countries like nz,usa,uk then source should be replaced as diff

Code:
diff_table_nz_final

Apart from nz,usa,uk it should be

Code:
source_table_<any_other_country>_final

any_other_country if eg if i pass as aus then it would source_table_aus_final

Code:
final_var=$(echo "source_table_@ctry_final"| sed -e "s/\@ctry/${ctry_input}/")

Basically would need to tweak the above code
# 10  
Old 08-20-2019
Code:
for ctry in de nz usa uk aus aut nl
  do    VLD=${ctry/${ctry/@($rplst)}} 
        SD=${VLD:+diff}
        final_var=${SD:-source}_table_${ctry}_final
        echo $final_var
done
source_table_de_final
diff_table_nz_final
diff_table_usa_final
diff_table_uk_final
source_table_aus_final
source_table_aut_final
source_table_nl_final

# 11  
Old 08-20-2019
Thanks RudiC for your help. But is there a way to tweak the existing code itself


Code:
final_var=$(echo "source_table_@ctry_final"| sed -e "s/\@ctry/${ctry_input}/")

why i am saying is because you if i give other than in that case only ctry only needs to be replaced

Code:
echo "other_table_@ctry_final"

I am trying to achieve madeingermany's method
Code:
final_var=$(echo "source_table_@ctry_final" | case $ctry_input in nz|usa|uk) sed "s/@ctry/$ctry_input/s/source_table/diff_table/";;*) sed "s/@ctry/$ctry_input/" ;; esac )


Last edited by Master_Mind; 08-21-2019 at 01:46 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace integer string in a variable based on month?

Hi Folks - Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64 I have a process that determines the start and end load periods for an Oracle data load process. The variables used are as follows follows: They are populated like such: However, the load requires the month to be the... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

How to find previous string based on an input string?

Hi, I did some research but cannot find the right solution so hopefully someone can help me here. I have a long string format like: VAR=111:aaaa,222:bbb,333:ccc it could be VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc what I looking for is eg. if I give ccc, it will return me 333... (2 Replies)
Discussion started by: netbanker
2 Replies

3. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

4. Shell Programming and Scripting

Replace string - searching from input file

Hi I need help with writing a script to change a string in a file. The script needs to read an input list (list.txt) file line by line searching for that string in a text.file. Once the string is found the last few words in the string should be replaced. eg list.txt will contain hello my... (6 Replies)
Discussion started by: sudobash
6 Replies

5. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

6. Shell Programming and Scripting

How to pad zeroes based on input string?

Hello everyone, I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine. But I want to compare two values which will come at 4 precision places. ex: file1 Date,Count,Checksum... (7 Replies)
Discussion started by: karthik adiga
7 Replies

7. Shell Programming and Scripting

Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records File2 has 2 fields. I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched) File2: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

8. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

9. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

10. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies
Login or Register to Ask a Question