Find and Replace Different Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and Replace Different Lines
# 1  
Old 12-22-2011
Find and Replace Different Lines

Hi, I am looking at modifiying a file but getting a bit lost with what i am trying to do.

I have the following file i need to alter. I want to search a list of files for the DEVSERIAL "0007862454" part but only the numbers. I then need to replace the line under DRIVES with the correct drive path.
Code:
NAME "Drive Name"
DESCRIPTION ""
HOST Host Name
POLICY SCSI-II
TYPE LTO-Ultrium
POOL "Default LTO-Ultrium"
LIBRARY "Library Name"
CLEANME
BUFFERS 32
DRIVES
        "/dev/rmt/s99mn"
        "49"
BLKSIZE 256
LOCKNAME  "Lock Name"
SANSTABLEADDR
DEVSERIAL "0007862454"
DEVICESUBTYPE ""
RESTOREDEVICEPOOL NO
COPYDEVICEPOOL NO

This is the script that i currently trying to play with but dont seem to be getting anywhere. I am i anywhere near the ball park on what i want to do or miles away.
Code:
for i in `drive_list`
do
   echo "Editing $i"
        awk '{ if ( $0 ~ /0007862454/ ) {
                  printf( "%s\n%s\n", "Some new inserted text here", $0 );
                  # could use the following to append
                  # printf( "%s\n%s\n", $0, "Some new appended text here" );
                } else {
                        print $0;
                }
        }' drive_list
done

Moderator's Comments:
Mod Comment Please use

code tags!

Last edited by vbe; 12-22-2011 at 10:08 AM.. Reason: pleas use code tags next time...
# 2  
Old 12-22-2011
find and replace word from all files

I have done one script for replacing all sysouts from my project. It may help you.
It will:
* Print before how was it
* Replace the needed Word alone
* Print after how it is.
* Print no of files changed

P.S : As sed -ie is used , a backup file with extension e will be created. So I wrote code to delete that too. I think sed -i only has to use. Anyway this script worked perfectly for me.
Take a backup of entire folder you wanna change. There is no guarantee from me.


I know this is not the right thing you asked.
Change what ever you need to.

Code:
#!/bin/bash

##########################################
#To  change System.out.println alone
##########################################


echo -e "To  change System.out.println alone `date` \n============================================================="
findString="System[ ]*.[ ]*out[ ]*.[ ]*println"
#findString="Session Destroyed"
replaceString="SomethingElse"
projectExactPath="/home/smily/ProjectPath"

echo -e "\nfindstring : $findString"
echo -e "replaceString : $replaceString"
echo -e "projectExactPath : $projectExactPath \n"

TotalNoOfFilesBefore=`find $projectExactPath -type f | wc -l`
isPresentFindNew=0
isPresentReplaceNew=0
isPresentReplace=

for filename in `find $projectExactPath -type f | grep -v "svn" | grep -E "\.java|\.jsp"`; 

do 

isPresentFind=`cat $filename | grep "$findString" | wc -l`

if [ $isPresentFind -ge "1" ] ; then

echo -e "\n\n"

echo -e "\nfilename : $filename"
echo -e "=====================================================================[>>>>>>>>"
echo -e "\n\tThe following\n---------------------------"
cat $filename | grep "$findString"

echo "Before : $isPresentFindNew"
isPresentFindNew=`echo "$isPresentFind + $isPresentFindNew" | bc`
echo "After : $isPresentFindNew"

echo "Replace command.........."
#################################REPLACE COMMAND#####################################
sed -ie "s/${findString}/${replaceString}/g" $filename
#################################REPLACE COMMAND#####################################

echo -e "\n\tChanged to : \n------------------------" 
cat $filename | grep "$replaceString"
echo -e "\n====================================================================<<<<<<<<]"

tempFile=$filename"e"
tempFilee=$filename"ee"
[ -f $tempFile ] && echo -e "\nDeleting \n$tempFile" && rm $filename"e" -f && echo "deleted"
[ -f $tempFilee ] && echo -e "\nDeleting \n$tempFilee" && rm $tempFilee -f && echo "deleted"
echo -e "\n\n"



isPresentReplace=`cat $filename | grep "$replaceString" | wc -l`
echo "Before : $isPresentReplaceNew"
isPresentReplaceNew=`echo "$isPresentReplace + $isPresentReplaceNew" | bc`
echo "After : $isPresentReplaceNew"

fi

done

TotalNoOfFilesAfter=`find $projectExactPath -type f | wc -l`

#Both should be same
echo -e "TotalNoOfFilesBefore Running script : $TotalNoOfFilesBefore"
echo -e "TotalNoOfFilesAfter Running Script  : $TotalNoOfFilesAfter"

echo -e "\n$findString is present or not [total=]: $isPresentFindNew"
echo -e "\n$replaceString is present or not [total=]: $isPresentReplaceNew"
echo -e "\n\n"

.
# 3  
Old 12-22-2011
Try this from within the directory your files are in:
Code:
 grep -l '0007862454' ./* | xargs awk '/DRIVES/{a=NR+1} a==NR{$0=FILENAME}{print > FILENAME".new"}'

This User Gave Thanks to kato For This Post:
# 4  
Old 12-22-2011
Alreay have a sed

I have now got that command working via command line perfectly. I just need to sort out the rest so that it uses variable to bring in the changes. Thank you for your help.

Now i understand the need to for the -v which is to allow in the variables. You just answered the question i was going to ask.

Been a great help. Spend many hours trying to work this out. So simple when you know how.

Last edited by forcefullpower; 12-22-2011 at 01:09 PM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 12-22-2011
If I understand you correctly, you want to specify the value to insert after DRIVES. Try this:
Code:
z="TEST"
grep -l '0007862454' ./* | xargs awk -v newDrive=$z '/DRIVES/{a=NR+1} a==NR{$0=newDrive}{print > FILENAME".new"}'

Then in your script, just set newDrive to any variable you have defined.

Last edited by kato; 12-22-2011 at 12:43 PM.. Reason: Adding more info
This User Gave Thanks to kato For This Post:
# 6  
Old 12-22-2011
Sorry... posted in a wrong thread...

Thanks all for ur replies, I solved the issue with the below command..


Code:
perl -i -ne 's;(\x1F)0\n;\x1F0Ç\n;g;print;' $rejectwoheader


Last edited by ratheeshjulk; 12-22-2011 at 09:15 PM.. Reason: posted in a wrong thread....
# 7  
Old 12-23-2011
It works how i want now but i need to get the file to overwrite the original file.

I tried using && FILENAME".new" FILENAME but i get error at the end of the awk command.

Code:
 syntax error The source line is 1.
 The error context is
                /DRIVES/{a=NR+1} a==NR{$0="        "new_drive_path}{print > FILENAME".new"} >>>  && <<<

Code:
while read line
do
        DRIVE_PATH=`echo "$line" | awk '{print $4}' | cut -c 2-20 | sed -e 's/\(.*\).$/\1/g`
        DRIVE_SERIAL=` echo "$line" | awk '{print $6}' | cut -c 2-11`
        grep -l "$DRIVE_SERIAL" ./* | xargs awk -v new_drive_path=$DRIVE_PATH '/DRIVES/{a=NR+1}
 a==NR{$0="        "new_drive_path}{print > FILENAME".new"}'                             
done < $DRIVESERIALANDPATHFILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line. File 1: hgacdavd sndm,ACNMSDC msgid "Rome" msgstr "" kgcksdcgfkdsb... (4 Replies)
Discussion started by: madira
4 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

8. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

9. Shell Programming and Scripting

find and replace three lines in files?

Hello, i usually use this for one tag or line: find . -type f -exec replace "whatever goes here" "" -- {} \; but i want to replace three lines in a file for example this: <script language=javascript>< ... (1 Reply)
Discussion started by: Bashar
1 Replies

10. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies
Login or Register to Ask a Question