find, copy and replace text in bash or sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find, copy and replace text in bash or sh
# 1  
Old 12-11-2008
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  
Old 12-11-2008
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;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash copy and paste text in file from one position to another

Hi I have a text file with lines beginning with 71303, 71403, 71602, I need to copy the 10 digit text at position 30 on lines beginning with 71303 (5500011446) to position 99 on every line beginning with 71602 (see example below), There may be many 71303 lines but I need the text copying to... (2 Replies)
Discussion started by: firefox2k2
2 Replies

2. Shell Programming and Scripting

Bash shell: Replace a text block by another

Hello, I'm an starter in Bash scripting. I would like to write a script in Bash shell that replaces a specific text block (a function) by another text block in a file: for example in my file --> $HOME/myFile.js replacing following function between other functions in the file: function ABC()... (6 Replies)
Discussion started by: om1559
6 Replies

3. Red Hat

copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file?? i tried to use something like: awk '{print "Hit the",$1,"with your",$2}' /aaa/qqqq.txt > uu.txt but i can't add an argument to point to the second file which i will replace in. please... (8 Replies)
Discussion started by: mos33
8 Replies

4. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

5. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

6. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

7. Shell Programming and Scripting

A simple find and replace without using any regex (bash)

Hi, I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string. For example, let's say the user enters I love "Unix" and the contents of the... (2 Replies)
Discussion started by: srikanths
2 Replies

8. UNIX for Dummies Questions & Answers

Find and replace text PLEASE HELP

Dear friends please help, I have a large data file with few hundred lines. A small example is shown below: datafile is a file with few hundred lines with the third column has many different character stings: test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh I need to... (6 Replies)
Discussion started by: bobo
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace text

test 100 abc test 134 bcd test 356 cdf test 831 dfg test 720 fgh Please advise how can I replace the abc, bcd....with ABC, BCD.... (1 Reply)
Discussion started by: bobo
1 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question