appending to sed output of one file into the middle of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting appending to sed output of one file into the middle of file
# 1  
Old 02-02-2007
Question appending to sed output of one file into the middle of file

hi,
i have a file

file1 file2
----------- -----------------
aa bbb ccc 111 1111 1111
ddd eee fff 222 3333 4444
ggg hhh iiii 555 6666 777

i select the portion of file1 as

sed -n '/aa/,/fff/p' file1 ,, this select first two lines of file1,

now i need to append these lines to the middle of file2 ie between line1 and line2

Please help me
# 2  
Old 02-02-2007
Code:
sed -n '/aa/,/fff/p' file1 > tmp
sed '1 r tmp' file2

# 3  
Old 02-02-2007
head -1 file2
sed -n '/aa/,/fff/p' file1
tail +2 file2
# 4  
Old 02-02-2007
Without using temp files

Code:
sed "1a\\
$(sed -n -e "/aa/,/fff/{/fff/ ! s/$/\\\\/;p; }" file1)
" file2

# 5  
Old 02-04-2007
slight modification

hi anbu23
that was a cute answer,,,, Smilie
but wat about the scenario, in which i replace any pattern space by some file contents.... Smilie
# 6  
Old 02-05-2007
Quote:
Originally Posted by go4desperado
hi anbu23
that was a cute answer,,,, Smilie
but wat about the scenario, in which i replace any pattern space by some file contents.... Smilie
You can use change command
Code:
sed "1c\\
$(sed -n -e "/aa/,/fff/{/fff/ ! s/$/\\\\/;p; }" file1)
" file2

Replace first line in pattern space by file1 contents
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending output to file

Hi, I want to calculate std dev for a list of files and then print the output appended to an existing file. I have a few folders within the directory Folder, and I am interested in getting the std dev of values in files named as time.txt. In the last pipe, if I print just the sd value, it... (1 Reply)
Discussion started by: jamie_123
1 Replies

2. Shell Programming and Scripting

Awk/Sed - appending within file

hello all, First time post here. I have searched a bit but could not find an exact answer. I have about a week's experience of Sed and Awk, and am having fun, but am a little stuck. I am reformatting an xml file into json format. I have got this far: {"clients": ...and so on. What I want... (22 Replies)
Discussion started by: singerfc
22 Replies

3. Shell Programming and Scripting

Appending script output to file

Hi guys, I have a script from which I would like its standard output results (upon execution) to be appended to a separate file (.txt format). How can I write such a command into the script for this to occur? I know there should be a >> involved somewhere. Thanks! (5 Replies)
Discussion started by: jjb1989
5 Replies

4. Shell Programming and Scripting

Appending process output into a file

Hello Friends, I'm trying to save process status of root user sorting by CPU usage. However i couldnt save the continuous, standard outputs into a file. Do you have any idea to do it? prstat -u root -a -s cpu | sed -e '/^$/d;/sleep/d;/Total/d' >> stat.txt >ls -l stat.txt -rw-r--r-- 1... (1 Reply)
Discussion started by: EAGL€
1 Replies

5. UNIX for Dummies Questions & Answers

redirect output into the middle of a file

If I want to cat one file and have the output inserted into a specific place on another file, how is this done? I know how to append >> and to overwrite > but say I have a file with: File1: abc def ghi jkl And a File with: File2: mno pqr stu vwx And I want to place the... (5 Replies)
Discussion started by: glev2005
5 Replies

6. UNIX for Dummies Questions & Answers

Appending something to output before being written to a file

Hi, I'm quite stuck with what I thought should've been simple but I just can't seem to do it. Firstly, I have the following done in bourne shell: cat datafile | tr '' '' >> newfile echo "$fullfilepath" >> newfile i want to have the output of that echo put on the same line as the output... (4 Replies)
Discussion started by: Darkst
4 Replies

7. Shell Programming and Scripting

appending a file using sed in ksh

i am trying to append a 5 line SGML file(file1) with a 500,000 line SGML file (file2). file1 is a template, so i wish to preserve. i only want to add lines 5 to the end of file2. i have: cp file1 temp1 sed -n '5,$p' file2 >> temp1 when i check the tail of temp1, i consistantly find the... (3 Replies)
Discussion started by: smac
3 Replies

8. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies

9. UNIX for Dummies Questions & Answers

SED Question -- on appending to a file

:confused: I have a script that Cats a flat database file which contains 12 columns into sed. I want to add a 13th column which includes " ,2005-08-29 " * The date needs to be the current date. This 13th column would be appended to the end of each line. Does anyone have a clue... (5 Replies)
Discussion started by: Redg
5 Replies
Login or Register to Ask a Question