Replace a string based on input


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Replace a string based on input

I am trying to read a value from a mapping file and would need to replace the value based on country parameter
Code:
source_table_@ctry_final
Expected
final_var=source_table_aus_final

Code:
If the country is in nz,usa,uk then 
final_var=diff_table_nz_final
final_var=diff_table_usa_final
like that

else
final_var=source_table_aus_final

Code:

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

# 2  
Please state the operating system, including version, and the shell you are using.

This works in ksh93 (Version AJM 93u+ 2012-08-01) on Ubuntu 18.04.3 LTS:
Code:
$ src=source_table_@ctry_final
$ rplst=nz,usa,uk
$ echo ${src/@ctry/{$rplst}}
source_table_nz_final source_table_usa_final source_table_uk_final

Strangely enough, it doesn't work with bash (4.4.20(1)-release):
Code:
$ echo ${src/ctry/{$rplst}}
source_table_{nz,usa,uk_final}

If anybody knows why, could we start a new thread on this, uh, feature?
[..]

Last edited by Scrutinizer; 3 Weeks Ago at 06:23 AM..
# 3  
Quote:
Originally Posted by apmcd47
...
Strangely enough, it doesn't work with bash (4.4.20(1)-release):
Code:
$ echo ${src/ctry/{$rplst}}
 source_table_{nz,usa,uk_final}

...

In bash, the unloved, deprecated eval helps:


Code:
eval echo ${src/@ctry/{$rplst\}}
 source_table_nz_final source_table_usa_final source_table_uk_final

and, no, the $ and { need to be in that sequence in order to make it a "brace expansion", which, in turn, can only be recognized / evaluated by the evil eval.
This User Gave Thanks to RudiC For This Post:
# 4  
But expected would be below. source string needs to be replaced with diff

Code:
diff_table_usa_final

Shell is bash
# 5  
Quote:
Originally Posted by Master_Mind
I am trying to read a value from a mapping file and would need to replace the value based on country parameter
How is that "country parameter" supplied and evaluated?
Quote:
Code:
source_table_@ctry_final
Expected
final_var=source_table_aus_final

The @ctry part has which value, for _aus_ to be part of the final string?

Quote:
Code:
If the country is in nz,usa,uk then 
 final_var=diff_table_nz_final
final_var=diff_table_usa_final
like that

else
 final_var=source_table_aus_final

Which table is relevant - diff_table or source_table - or is the distinction part of the problem?



Quote:
Code:

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

What is and which value has ctry_input? Where does it come into play?
# 6  
It is because of the parsing order of the shell. In bash brace expansions are performed before any of the other expansions, hence the need for eval (usual caution applies (security)!) for a second round of expansions after the first round.

Last edited by Scrutinizer; 3 Weeks Ago at 06:35 AM..
# 7  
Unlike bash, ksh93 seems to do variable expansion before brace expansion. (Scrutinizer just said it.)

Master_Mind, I am not fully getting your requirement, but here is how you can adjust the ctry_input variable
Code:
#!/bin/sh
ctry_input=$1
case $ctry_input in
nz|usa|uk)
;;
*)
  ctry_input=aus
;;
esac
sed "s/@ctry/$ctry_input/"

This sample script you can run with a "uk" or "usa" argument, for example
Code:
sh sample.sh uk < inputfile

--- Post updated at 12:39 ---

To meet your sourc/diff replacement requirement, you can move the sed into the case-branch:
Code:
#!/bin/sh
ctry_input=$1
case $ctry_input in
nz|usa|uk)
  sed "s/@ctry/$ctry_input/
  s/source_table/diff_table/"
;;
*)
  ctry_input=aus
  sed "s/@ctry/$ctry_input/"
;;
esac

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

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

Featured Tech Videos