Need to find a string, check the next line, and if it matches certain criteria, replace it with a s


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to find a string, check the next line, and if it matches certain criteria, replace it with a s
# 1  
Old 11-16-2009
Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

Hey Fellas.

I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv file. Roughly about 1500 times in the file. I have been trying to do this with awk/sed, but i haven't had the best of luck.
example:
find the string that starts with data7
if the string on the next line has the word prisoner then replace it with the string in line one on the csv file.
the next instance of the pattern would be replaced by the string in line 2 of the csv file. and so on.
the word prisoner appears multiple times in the file, but i only want to replace the ones that directly follow the string containing "data7"
does this make sense?

When i say i'm brand new to scripting i mean BRAND new. I'm taking some classes, but i need to get this done as soon as possible and what i have learned so far hasn't gotten me much closer. any help ya'll can give would be greatly appreciated.
Thanks!
# 2  
Old 11-16-2009
show your code, and your input csv files.
# 3  
Old 11-16-2009
I don't have my linux box handy, and thats where all my code snippets are, i'll post them when i get home tonight. Here are some examples of the files:
File 1:
Code:
Type: SystemDeclarationData;7   #inthis example we'll use the "JDLM"  the first 
1BN22IN JDLM CLASS I IV VIII SP         #string i would need to find would be the 
                                                            #"SystenDeclarationData" string.  The next 
                                                            #line contains "JDLM", so i would need to 
                                                             #replace it.  Notice that the term "jdlm"
                                                             #appears elsewhere in the file.  these 
                                                             #instances need to stay as is.  




U0010000003

JDLM CLASS I IV VIII SP

















1
1
25
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

Type: SystemDeclarationData;7
1BN22IN JDLM CLASS V SP





U0010000004

JDLM CLASS V SP

















1
1
26
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

i don't have a copy of the csv file yet. as far as i know it will only consist of two columns.

Code:
NAME  
last, first mi    

NUMBER
this will be a 5-10 digit number

There will be about 1500 instances that will need changed, if not more.
I appreciate the quick replies, I'll post the code that i have as soon as i get home tonight.
Thanks again
# 4  
Old 11-16-2009
Try this (but a sample could help !)
Code:
#!/bin/bash
Input="data.csv"
I=1
while read LINE
do    #~ find the string that starts with data7
    if [ "${LINE:0:5}" = "data7" ]
    then
        read LINE    #~ if the string on the next line has the word prisoner then replace it with the string in line 1.
        if $(echo "$LINE" | grep -q prisoner)
        then
            head -n$I "$Input" | tail -n1
            (( I ++ ))    #~ the next instance of the pattern would be replaced by the string in line 2 and so on.
        else echo "$LINE"
        fi
    else echo "$LINE"
    fi
done < "$Input" > OutputFile

# 5  
Old 11-16-2009
Thanks FRANS! This looks a lot simpler than I thought it would be. I'll try it out as soon as i get on my linux box.
I for the most part understand whats happening here. Could you explain the purpose of the ":0:5" in this line? I found what the curly brackets do, but as of yet, not the colons.

Code:
 if [ "${LINE:0:5}" = "data7" ]

Thanks for all the help!

---------- Post updated at 12:05 PM ---------- Previous update was at 10:43 AM ----------

As far as samples go, i have a file (format unknown). There are about 15000 lines or so that contain the word "prisoner". Each prisoner will be assigned a number that will be pulled from a .csv file. The input file looks like this (only right around 44000 lines long):

Code:
Type: SystemDeclarationData;7    
1BN22IN prisoner



U0010000003

prisoner VIII SP

















1
1
25
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

Type: SystemDeclarationData;7
1BN22IN prisoner CLASS V SP





U0010000004

prisoner CLASS V SP

















1
1
26
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

The output would then look something like this:
Code:
Type: SystemDeclarationData;7    
100000056



U0010000003

prisoner VIII SP

















1
1
25
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

Type: SystemDeclarationData;7
20000056





U0010000004

prisoner CLASS V SP

















1
1
26
-1
0
0
0
0
0
Type: SupplyLoad;4
0
0
end;

end;

Notice that only the "prisoner" string underneath the "Type: SystemDeclarationData;7" string changed.
The .csv file will end up being either a one or two column file depending on whether or not they want names along with the numbers.
I don't know if that helps or not. don't have much to contribute as far as a real sample goes at the moment. They pretty much gave me basic structure, a goal, and said have fun. heh.

---------- Post updated at 01:03 PM ---------- Previous update was at 12:05 PM ----------

Frans:
We are on the right track!! I modified the scropt that you posted a little bit:
Code:
#!/bin/bash
changefile="test_csv.csv"         #this is the file the new string is pulled from
Input="test.fplan"    #this is the file that needs changed
I=1

while read LINE
do    #~ find the string that starts with data7
    if [[ "${LINE}" =~ "SystemDeclarationData" ]]
    then
        read LINE    #~ if the string on the next line has the word prisoner then replace it with the string in line 1.
        if [[ "${LINE}" =~ "JDLM" ]]
        then
            head -n$I "$changefile" | tail -n1
            (( I ++ ))    #~ the next instance of the pattern would be replaced by the string in line 2 and so on.
        else echo "$LINE"
        fi
    else echo "$LINE"
    fi
done < "$Input" > /home/bj/Desktop/test_complete

The only problem i'm having is that it's deleting the line that contains "SystemDeclarationData". otherwise it's working great. can you tell me what i'm doing wrong?
# 6  
Old 11-16-2009
Here is it (not tested)

Quote:
Originally Posted by midniteslice
Thanks FRANS! This looks a lot simpler than I thought it would be. I'll try it out as soon as i get on my linux box.
I for the most part understand whats happening here. Could you explain the purpose of the ":0:5" in this line? I found what the curly brackets do, but as of yet, not the colons.
Code:
 if [ "${LINE:0:5}" = "data7" ]

it extracts 5 characters from position 0 (the first)
Quote:
The only problem i'm having is that it's deleting the line that contains "SystemDeclarationData". otherwise it's working great. can you tell me what i'm doing wrong?
The script is wrong i'll modify it to work properly.
In a couple of minutes, OK?Smilie
The script
Code:
#!/bin/bash
Input="test.fplan"
I=1
while read LINE
do    #~ find the string that starts with data7
    echo "$LINE"
    [ "${LINE:0:5}" =~ "SystemDeclarationData" ] || continue #~ continue if the condition is not satisfied
    read LINE
    if $(echo "$LINE" | grep -q prisoner)
    then    head -n$I $Input | tail -n1
        (( I ++ ))
    else echo "$LINE"
    fi
done < $Input > test_csv.csv


Last edited by frans; 11-16-2009 at 02:37 PM.. Reason: script provided
# 7  
Old 11-16-2009
I got it working with an echo command. essentially, i guess both are doing the same thing, just outputting in a different format?. Yours is a whole lot cleaner!! heh. I can't thank you enough for the help. Credit goes to you my friend!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace matches string in each line with the arrayvalue in shell

I have a list of value , and need to replace that in my file. Eg: File1 tcname:fail tcname: Pass tcname:skipped File2: 01,02,03 Output: File 1 01:fail 02: Pass 03:Skipped (8 Replies)
Discussion started by: DevAakash
8 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

4. Shell Programming and Scripting

Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly. This is the file I have: while IFS=',' read -r f1 f2 f3 do { nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out & } done < "/home/testuser/dbmaintenance/week1offlineserver.txt" In... (4 Replies)
Discussion started by: singhhe
4 Replies

5. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

6. Shell Programming and Scripting

Find and replace string matching criteria

Dear Friends, I am looking for a way to replace a string (multiple lines) starting with something and ending with something (these two values do not change) with blank. Basically I want to delete this code injection accross many sites and folders. Search Code (across files and folders) that... (2 Replies)
Discussion started by: libras
2 Replies

7. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

8. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

9. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies
Login or Register to Ask a Question