sed replace characters in next line with input from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace characters in next line with input from a file
# 1  
Old 07-08-2011
sed replace characters in next line with input from a file

Hi,
I have a set of strings in filea.
I want to search string xyz in fileb and replace next line in file b with the content from filea.

Code:
#cat filea
abc
def
ghi
 
#cat fileb
asdkjdslka
sajljskdjoi
xyzjjjjkko
aaaaaaaa
bbbbbbbb
cccccccc
xyzsdsajd
dddddddd
eeeeeeee
fffffffffsjk
xyzsdaioll
iiiiiiiiiiiijjjjjjj
kkkkkkkkk
mmmmmm
 
Expected fileb after operation:
asdkjdslka
sajljskdjoi
xyzjjjjkko
aaaaaaaa
bbbbbabc
cccccccc
xyzsdsajd
dddddddd
eeeeedef
fffffffffsjk
xyzsdaioll
iiiiiiiiiiiijjjjjjj
kkkkkkghi
mmmmmm

# 2  
Old 07-08-2011
Hi,

Test next 'perl' script:
Code:
$ cat script.pl
use warnings;
use strict;

my $filea_name = $ARGV[0] || "";
my @filea;
my $modline = 0;
while ( <> ) {
        chomp;
        do { push @filea, $_; next } if $filea_name eq $ARGV;

        ## Here only lines of 'fileb' file

        if ( index( $_, "xyz" ) > -1 ) {
                $modline = $. + 2;
        }

        if ( $modline == $. ) {
                s/(?:\S{3})(\s*)$/shift( @filea ) . $1/e;
                $modline = 0;
        }

        printf "%s\n", $_;
}
$ perl script.pl filea fileb
asdkjdslka
sajljskdjoi
xyzjjjjkko
aaaaaaaa
bbbbbabc
cccccccc
xyzsdsajd
dddddddd
eeeeedef
fffffffffsjk
xyzsdaioll
iiiiiiiiiiiijjjjjjj
kkkkkkghi
mmmmmm

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 07-08-2011
awesome! Smilie
Thanks Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to replace new line characters in a huge file

Hi , I would like to replace new line characters(\n) in a huge file of about 2 million records . I tried this one (:%s/\n//g) but it's hanging there and no result. Does this command do not work if the file is big. Please let me know if you have any other options Regards Raj (1 Reply)
Discussion started by: rajeevm
1 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

4. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

5. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

6. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

7. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

8. Shell Programming and Scripting

sed to remove 1st two characters every line of text file

what is the sed command to remove the first two characters of every line of a text file? each line of the text file has the same amount of characters, and they are ALL NUMERIC. there are hundreds of lines though. for example, >cat file1.txt 10081551 10081599 10082234 10082259 20081134... (20 Replies)
Discussion started by: ajp7701
20 Replies

9. Shell Programming and Scripting

How to replace characters 7 through 14 of every line in a file

Hi all, I have a file with multiple lines. I want to replace characters 7 through 14 of every line with 0000000 Input: 12345678901234567890 23456789012345678901 Output 12345600000004567890 23456700000005678901 Please help. JaK (9 Replies)
Discussion started by: jakSun8
9 Replies

10. Shell Programming and Scripting

sed not outputting last line of input file

I am using sed for a simple substitution (see command syntax below). Everything works fine except that the last line of the input file does not get written to the output file. Has anyone ever seen this and know of way to force the last line to be written? I don't know if it's playing a part in... (3 Replies)
Discussion started by: 2reperry
3 Replies
Login or Register to Ask a Question