Find and Replace Different Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and Replace Different Lines
# 8  
Old 12-23-2011
I don't believe you can overwrite the original file without causing a problem. Although this did work for me:

Code:
{print > FILENAME}

But to be safe, you can move the new file to the old file after awk has finished. So after the print block append:

Code:
END{system("mv "FILENAME".new "FILENAME)}

So for your code:
Code:
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"}END{system("mv "FILENAME".new "FILENAME)}'

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
Find::Lib(3pm)						User Contributed Perl Documentation					    Find::Lib(3pm)

NAME
Find::Lib - Helper to smartly find libs to use in the filesystem tree VERSION
Version 1.01 SYNOPSIS
#!/usr/bin/perl -w; use strict; ## simple usage use Find::Lib '../mylib'; ## more libraries use Find::Lib '../mylib', 'local-lib'; ## More verbose and backward compatible with Find::Lib < 1.0 use Find::Lib libs => [ 'lib', '../lib', 'devlib' ]; ## resolve some path with minimum typing $dir = Find::Lib->catdir("..", "data"); $path = Find::Lib->catfile("..", "data", "test.yaml"); $base = Find::Lib->base; # or $base = Find::Lib::Base; DESCRIPTION
The purpose of this module is to replace use FindBin; use lib "$FindBin::Bin/../bootstrap/lib"; with something shorter. This is specially useful if your project has a lot of scripts (For instance tests scripts). use Find::Lib '../bootstrap/lib'; The important differences between FindBin and Find::Lib are: o symlinks and '..' If you have symlinks in your path it respects them, so basically you can forget you have symlinks, because Find::Lib will do the natural thing (NOT ignore them), and resolve '..' correctly. FindBin breaks if you do: use lib "$Bin/../lib"; and you currently are in a symlinked directory, because $Bin resolved to the filesystem path (without the symlink) and not the shell path. o convenience it's faster too type, and more intuitive (Exporting $Bin always felt weird to me). DISCUSSION
Installation and availability of this module The usefulness of this module is seriously reduced if Find::Lib is not already in your @INC / $ENV{PERL5LIB} -- Chicken and egg problem. This is the big disavantage of FindBin over Find::Lib: FindBin is distributed with Perl. To mitigate that, you need to be sure of global availability of the module in the system (You could install it via your favorite package managment system for instance). modification of $0 and chdir (BEGIN blocks, other 'use') As soon as Find::Lib is compiled it saves the location of the script and the initial cwd (current working directory), which are the two pieces of information the module relies on to interpret the relative path given by the calling program. If one of cwd, $ENV{PWD} or $0 is changed before Find::Lib has a chance to do its job, then Find::Lib will most probably die, saying "The script cannot be found". I don't know a workaround that. So be sure to load Find::Lib as soon as possible in your script to minimize problems (you are in control!). (some programs alter $0 to customize the display line of the process in the system process-list ("ps" on unix). (Note, see perlvar for explanation of $0) USAGE
import All the work is done in import. So you need to 'use Find::Lib' and pass a list of paths to add to @INC. See "BACKWARD COMPATIBILITY" section for more retails on this topic. The paths given are (should) be relative to the location of the current script. The paths won't be added unless the path actually exists on disk base Returns the detected base (the directory where the script lives in). It's a string, and is the same as $Find::Lib::Base. catfile A shorcut to File::Spec::catfile using Find::Lib's base. catdir A shorcut to File::Spec::catdir using Find::Lib's base. BACKWARD COMPATIBILITY
in versions <1.0 of Find::Lib, the import arguments allowed you to specify a Bootstrap package. This option is now removed breaking backward compatibility. I'm sorry about that, but that was a dumb idea of mine to save more typing. But it saves, like, 3 characters at the expense of readability. So, I'm sure I didn't break anybody, because probabaly no one was relying on a stupid behaviour. However, the multiple libs argument passing is kept intact: you can still use: use Find::Lib libs => [ 'a', 'b', 'c' ]; where "libs" is a reference to a list of path to add to @INC. The short forms implies that the first argument passed to import is not "libs" or "pkgs". An example of usage is given in the SYNOPSIS section. SEE ALSO
FindBin, FindBin::libs, lib, rlib, local::lib http://blog.cyberion.net/2009/10/ive-done-something-bad-i-broke-backward-compatibility.html <http://blog.cyberion.net/2009/10/ive-done-something-bad-i-broke-backward-compatibility.html> AUTHOR
Yann Kerherve, "<yann.kerherve at gmail.com>" BUGS
Please report any bugs or feature requests to "bug-find-lib at rt.cpan.org", or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Find-Lib <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Find-Lib>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. ACKNOWLEDGEMENT
Six Apart hackers nourrished the discussion that led to this module creation. Jonathan Steinert (hachi) for doing all the conception of 0.03 shell expansion mode with me. SUPPORT &; CRITICS I welcome feedback about this module, don't hesitate to contact me regarding this module, usage or code. You can find documentation for this module with the perldoc command. perldoc Find::Lib You can also look for information at: o AnnoCPAN: Annotated CPAN documentation http://annocpan.org/dist/Find-Lib <http://annocpan.org/dist/Find-Lib> o CPAN Ratings http://cpanratings.perl.org/d/Find-Lib <http://cpanratings.perl.org/d/Find-Lib> o RT: CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Find-Lib <http://rt.cpan.org/NoAuth/Bugs.html?Find-Lib> o Search CPAN http://search.cpan.org/dist/Find-Lib <http://search.cpan.org/dist/Find-Lib> COPYRIGHT &; LICENSE Copyright 2007, 2009 Yann Kerherve, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2011-09-17 Find::Lib(3pm)