Search for multiple string and replace with respective values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for multiple string and replace with respective values
# 1  
Old 03-02-2011
Search for multiple string and replace with respective values

Hi,


Can anyone help me to search for multiple strings within specified position and replace with respective string value.
For example I need to search the string from the position 11 to 20 and if it contain ABC and then replace it by BCDEFGHIJ ... find AABZSDJIK and replace with QWE. and so on.. The file may have 50000 records and I need to search and replace with 10 different values. I can't use perl script please let me know the best way to achive this in UNIX script.
# 2  
Old 03-02-2011
Try using a sed script, e.g...
Code:
$ head file1 script1.sed
==> file1 <==
xxxxxxxxxxABCxxxxxxxxxxxxxxxxx
yyyyyyyyyyAABZSDJIKyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

==> script1.sed <==
s/\(..........\)ABC/\1BCDEFGHIJ/
s/\(..........\)AABZSDJIK/\1QWE/

$ sed -f script1.sed file1 > file2

$ cat file2
xxxxxxxxxxBCDEFGHIJxxxxxxxxxxxxxxxxx
yyyyyyyyyyQWEyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

$

# 3  
Old 03-07-2011
Thanks for your reply. My requirement is slightly different. Search for different set of string and replace with respective value with accurate field position. For example i want to search specific set of strings from 25 to 35 and repalce with corresponding values. If replacing value has 10 character then remaining 5 character should be filled with space so that the next value starting from 36 is not shifted to left column.

Code:
 
find from 25 to 35  -- replace with
2FMDK38C39B--AAAAAAAAAAA
1FMEU64    --BBBB       
5LMFU275   --CCCCC      
1ZVHT82H28 --DD         
           --Nodata



Code:
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
3FADP4CJ6BM103485FP8FC022FMDK38C39B2010-08-212010-10-20
4M2EU38807UJ02689VT0ZD0M1FMEU64    2008-02-292010-12-27
1FMEU64E39UA36544NS1CD0M5LMFU275   2009-07-042010-12-21
1FAHP2EW0AG131631OQ9AA1A1ZVHT82H28 2010-01-162010-12-21
1FMEU3BE6AUF00576ER9AA1A           2010-08-072010-12-23
1FMEU33E18UB03690FR9AA1A           2008-11-082010-12-23


Code:
 
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
3FADP4CJ6BM103485FP8FC02AAAAAAAAAAA2010-08-212010-10-20
4M2EU38807UJ02689VT0ZD0MBBBB       2008-02-292010-12-27
1FMEU64E39UA36544NS1CD0MCCCCC      2009-07-042010-12-21
1FAHP2EW0AG131631OQ9AA1ADD         2010-01-162010-12-21
1FMEU3BE6AUF00576ER9AA1ANodata     2010-08-072010-12-23
1FMEU33E18UB03690FR9AA1ANodata     2008-11-082010-12-23

# 4  
Old 03-11-2011
Can anyone help me to get above result.
# 5  
Old 03-11-2011
Hi.

If you are doing this yourself, perhaps this is something you can build upon. This would need to read in the pairs of strings, old and new, then search each line for old in the second field:
Code:
#!/usr/bin/env bash

# @(#) s4	Demonstrate FIELDWIDTH and substitution.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
# Context for local environment.
[ -f ~/bin/context ] && . ~/bin/context awk

FILE=${1-data1}

pl " Data file $FILE:"
cat $FILE

# 1FMEU64    --BBBB
pl " Sample results, awk with FIELDWIDTHS, first pattern only:"
awk '
BEGIN	{ FIELDWIDTHS = " 24 11 80" ; print "FIELDWIDTHS =", FIELDWIDTHS }
$2 ~ /1FMEU64/	{ print ; $2 = "BBBB" ; l = length($3) 
					printf("%24s%-11s%-*s\n",$1,$2,l,$3) }
' $FILE

exit 0

producing:
Code:
% ./s4

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.32-5-686, i686
Distribution        : Debian GNU/Linux wheezy/sid 
GNU bash 4.1.5
GNU Awk 3.1.7

-----
 Data file data1:
3FADP4CJ6BM103485FP8FC022FMDK38C39B2010-08-212010-10-20
4M2EU38807UJ02689VT0ZD0M1FMEU64    2008-02-292010-12-27
1FMEU64E39UA36544NS1CD0M5LMFU275   2009-07-042010-12-21
1FAHP2EW0AG131631OQ9AA1A1ZVHT82H28 2010-01-162010-12-21
1FMEU3BE6AUF00576ER9AA1A           2010-08-072010-12-23
1FMEU33E18UB03690FR9AA1A           2008-11-082010-12-23

-----
 Sample results, awk with FIELDWIDTHS, first pattern only:
FIELDWIDTHS =  24 11 80
4M2EU38807UJ02689VT0ZD0M1FMEU64    2008-02-292010-12-27
4M2EU38807UJ02689VT0ZD0MBBBB       2008-02-292010-12-27

Not all versions of gawk that I have encountered handle FIELDWIDTHS correctly.

I think using the builtin function unpack in perl might an alternate method.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

5. 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

6. Shell Programming and Scripting

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question