Script Search replace - complicated


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Search replace - complicated
# 1  
Old 11-25-2009
Wrench Script Search replace - complicated

I have a text file for which i need a script which does some fancy search and replace.
Basically i want to loop through each line, if i find an occurance of certain string format then i want to carry on search on replace another line, once i replaced this line i will contine to search for the previous string format once i find it i will again replace the next line with a specific format.
Eg

CODE A_123
TEXT
TEXT
VAL 1,2,3
TEXT
CODE A_321
TEXT
TEXT
VAL 1,2,3XT
In this example i want to say find CODE A*, if i find this look for VAL* and replace with X. Continue doing this for the whole file
# 2  
Old 11-25-2009
Please give an example of desired output
# 3  
Old 11-25-2009
CODE A_123
TEXT
TEXT
VAL 1,2,3
TEXT
CODE A_321
TEXT
TEXT
VAL 1,2,3
would become:

CODE A_123
TEXT
TEXT
VAL 1,5,2
TEXT
CODE A_321
TEXT
TEXT
VAL 5,6,7

Basically i only replace the VAL values if i have previously encountered a CODE A_*
# 4  
Old 11-25-2009
bash
Code:
while read -r line
do
    case "$line" in 
        "CODE A"*)  flag=1;;
    esac
    if [ "$flag" -eq 1 ];then
        case "$line" in
            VAL* ) echo "VAL 5,6,7";flag=0;continue;;
            * ) echo $line;;
        esac    
    fi
    [ "$flag" -eq 0 ] && echo $line
done <"file"

output
Code:
$ more file
CODE A_123                        
TEXT
TEXT
VAL 1,2,3
TEXT
CODE A_321
TEXT
TEXT
VAL 1,2,3
TEXT
TEXT
VAL 1,2,3
TEXT
END
CODE A_321
TEXT
TEXT
VAL 1,2,3

$ ./shell.sh
CODE A_123
TEXT
TEXT
VAL 5,6,7
TEXT
CODE A_321
TEXT
TEXT
VAL 5,6,7
TEXT
TEXT
VAL 1,2,3
TEXT
END
CODE A_321
TEXT
TEXT
VAL 5,6,7

# 5  
Old 11-25-2009
Thanks that works perfectly. Instead of echoing how can i write all the contents to a file.
# 6  
Old 11-25-2009
Use output redirection.

If the input becomes large that may become slow. If so, post again here and we can talk about using sed, awk, or perl to accomplish this instead. (All three can do it and in less lines of code in most cases.)
# 7  
Old 11-25-2009
Code:
perl -pe '$ON = 1 if /CODE A.+/; $ON = 0 if ( $ON && s/VAL 1,2,3/VAL 4,5,6/ )' datafile > replaced

There is the less line of Perl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script for search and replace by field

Hi, I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern. Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file. Input... (13 Replies)
Discussion started by: chandrath
13 Replies

2. Shell Programming and Scripting

Search and replace script

Hi, Below is the script which will find a particular text and replace with another one in a group of files under a directory /test #!/bin/bash old=$1 --- first input old text new=$2--- input new text cd /test --- folder into which files need to be checked for y in `ls *`; do sed... (2 Replies)
Discussion started by: chetansingh23
2 Replies

3. Shell Programming and Scripting

Script to search and replace

Hi All, I am trying to write a script which will find a particular text in certain group of files under a directory and if found correctly it will replace them with a new text in all the files. Could any one let me know how do i find the text in many files under a directory. Thanks (3 Replies)
Discussion started by: chetansingh23
3 Replies

4. Shell Programming and Scripting

Search complicated strings on file

Can someone help me? I been figuring out how I can search and extract a complicated search string from a file. The whole string is delimited by a period. And the file where I'm searching is composed of differnt string such as that. For example, I have this search string: and I have a file... (3 Replies)
Discussion started by: Orbix
3 Replies

5. Shell Programming and Scripting

Please Help to Check script Search and Replace

Please Help to Check script Search and Replace Ex. Search 0001 and Replete un_0001 ---script Code: nawk -F\" 'NR==FNR{a;next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt script is work to one result but if i have file1.txt, file2.txt, file3.txt i want to Replace... (5 Replies)
Discussion started by: kittiwas
5 Replies

6. Emergency UNIX and Linux Support

Complicated SED search required

Hi All, I'm trying to extract all the description fields from a MIB file which contain multiple instances of the following text: ENTERPRISE compaq VARIABLES { sysName, cpqHoTrapFlags, cpqSsBoxCntlrHwLocation, cpqSsBoxCntlrIndex, cpqSsBoxBusIndex,... (10 Replies)
Discussion started by: badoshi
10 Replies

7. UNIX for Dummies Questions & Answers

Unix script, sed search and replace?

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies

8. Shell Programming and Scripting

complicated search within file

Hi, I have following problem. I have a file with time stamps and some data describing what happened between time stamps. Something like this: 10:00 meeting with K meeting with L 11:00 lunch 12:00 work with K 13:00 From this file I have to get a file with... (7 Replies)
Discussion started by: mmike
7 Replies

9. UNIX for Dummies Questions & Answers

Perl search and replace not working in csh script

I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script perl -pi -e 's@/Pattern@@g' $path/$file I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies

10. UNIX for Dummies Questions & Answers

multiple input search and replace script

hi, i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files. cho "" echo... (3 Replies)
Discussion started by: tungaw2004
3 Replies
Login or Register to Ask a Question