The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find text but replace a text beside it The One Shell Programming and Scripting 4 09-02-2008 03:13 AM
bash find and remove text Movomito Shell Programming and Scripting 5 04-05-2008 01:48 PM
A simple find and replace without using any regex (bash) srikanths Shell Programming and Scripting 2 03-18-2008 08:08 AM
Find and replace text PLEASE HELP bobo UNIX for Dummies Questions & Answers 6 01-12-2006 05:16 PM
Find and replace text bobo UNIX for Dummies Questions & Answers 1 01-12-2006 01:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-11-2008
jmvbxx jmvbxx is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 4
find, copy and replace text in bash or sh

here is my prob ..

i have a very large text files and i need to locate specific lines, copy them and then replace a single word in the replaced text

example

find all lines that contain '/etc', copy the line immediately below (not at the end of the file) and then replace '/etc' with '/root' in the new line

old text:
rsync --delete -avpz xxx.xxx.xxx.xxx:/var /usr/backup/123 > /usr/backup.log
rsync --delete -avpz xxx.xxx.xxx.xxx:/usr/local/etc /usr/backup/123/loc > /usr/backup.log
rsync --delete -avpz xxx.xxx.xxx.xxx:/etc /usr/backup/123 > /usr/backup.log

new:
rsync --delete -avpz xxx.xxx.xxx.xxx:/var /usr/backup/123 > /usr/backup.log
rsync --delete -avpz xxx.xxx.xxx.xxx:/usr/local/etc /usr/backup/123/loc > /usr/backup.log
rsync --delete -avpz xxx.xxx.xxx.xxx:/etc /usr/backup/123 > /usr/backup.log
rsync --delete -avpz xxx.xxx.xxx.xxx:/root /usr/backup/123 > /usr/backup.log

i hope my request for help is not too convoluted .. thank you so much in advance!!!


j
  #2 (permalink)  
Old 12-11-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,086
Hi, You may try perl:

#! /usr/bin/perl

Code:
open FH,"<a.txt";
@arr=<FH>;
map {tr/\n//d} @arr;
for($i=0;$i<=$#arr;$i++){
	print $arr[$i],"\n";
	if ($arr[$i]=~m/etc/){
		print $arr[$i+1],"\n" if ($i+1 <= $#arr);
		$arr[$i+1]=~s/etc/root/ if ($i != $#arr);	
	}
}
close FH;
Closed Thread

Bookmarks

Tags
bash, copy, find, replace

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0