Applying sed against a file from a list of values stored in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Applying sed against a file from a list of values stored in a variable
# 1  
Old 07-24-2015
Applying sed against a file from a list of values stored in a variable

Hi Forum.

I have the following challenge at work that I need to write a script for.

I have a file abc.txt with the following contents:

Code:
4560123456
4570987654
4580654321

I want to be able to search/replace in abc.txt - the first 4 characters anything starting with 4560 to 7777; 4570 to 8888; 4580 to 9999

I want to use a variable (if possible since in the future the list could be expanded and I would only need to expand the variable values without any additional coding):
Code:
str_search_list="4560/7777;4570/8888;4580/9999"

How would I loop thru the search string list and run the sed command on abc.txt?

Last edited by Scrutinizer; 07-24-2015 at 06:46 PM..
# 2  
Old 07-24-2015
With a bit of bash / ksh93 / zsh parameter expansion, try:
Code:
sed "s/^${str_search_list//;//;s/^}/" file

Result:
Code:
7777123456
8888987654
9999654321


Last edited by Scrutinizer; 07-24-2015 at 06:49 PM..
# 3  
Old 07-25-2015
Thanks I will give it a try
# 4  
Old 07-26-2015
Also a sed multi-liner is easy to extend
Code:
sed "
s/^4560/7777/
s/^4570/8888/
s/^4580/9999/
" abc.txt

You can put the sed script to a variable
Code:
sedscript="
s/^4560/7777/
s/^4570/8888/
s/^4580/9999/
"
sed "$sedscript" abc.txt

or to a sedfile
Code:
sed -f sedfile abc.txt


Last edited by MadeInGermany; 07-26-2015 at 05:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that (once the leading zero is removed) will match a line in link. That is the lint to print in output.... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. UNIX for Dummies Questions & Answers

List of All Users Stored in Variable

I'm trying to get a list of all users on the system, store the list in a variable, then find all the files of a certain extension owned by each of the users. The command I use to get the list is getent passwd | cut -d: -f1 | sortand this works just fine. However, if I try to store it in a... (1 Reply)
Discussion started by: hobscrk777
1 Replies

4. Red Hat

How to check values stored in variable?

hey, i have stored values of query like this val_2=$( sqlplus -s rte/rted1@rel75d1 << EOF set heading off select source_id from cvt_istats where istat_id > $val_1; exit EOF ) echo $val_2 now , val_2 has five values displayed like this 1 2 3 4 5 what i have to do is to check the... (1 Reply)
Discussion started by: ramsavi
1 Replies

5. Programming

Why this const variable is not changing even after applying const_cast?

Hi In the following code, why the variable 'i' still retains the value 10 instead of 11? Why it is not possible to alter this variable's value? int main() { const int i = 10; *(const_cast<int*>(&i)) = 11; cout << i << endl; // Ans: 10 } (6 Replies)
Discussion started by: royalibrahim
6 Replies

6. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

7. Shell Programming and Scripting

how to use the filehandle stored in a variable without knowing its file association

how to use the filehandle stored in a variable without knowing its file association i.e. the filename code my $logFH = $connObj->get('logFH'); infoPrint("Variable is of type IO \n") if(UNIVERSAL::isa($logFH, 'IO')); infoPrint("$logFH\n"); output == INFO :: Variable is of type... (0 Replies)
Discussion started by: rrd1986
0 Replies

8. Emergency UNIX and Linux Support

Comparing date values stored in a file

Hi all, I have written a script which stores data along with requisite dates in a file. I need to compare the stored date values in the file to obtain the row with the highest date value i.e. the recent most entered record in the file. Please help cause i dont know how we can compare dates in... (10 Replies)
Discussion started by: sumi_mn
10 Replies

9. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

10. Shell Programming and Scripting

Increment variable stored in a file

Hi, I want to write a perl script, which will increment number stored in file. I want to do this without any file handles. I think we have to use some UNIX commands. I am not sure how to do this with file handles. Thanks, (1 Reply)
Discussion started by: solitare123
1 Replies
Login or Register to Ask a Question