Insert output into file at line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert output into file at line number
# 1  
Old 11-17-2009
Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file.
I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this.
# 2  
Old 11-17-2009
Hi ,
Please try below code

outfilename=`ls -1 *.out`
if [ $? -eq 0 ]
then
for i in `cat $outfilename`
do
txtfilename=`grep -l $i *.txt`
linenumber=`grep -n $i $txtfilename|cut -f1 -d ":" `
sed -i "$linenumber"'H' $txtfilename
done

fi
# 3  
Old 11-17-2009
Simple way is write a two line shell script.

Something like this,

Code:
$ cat  t
a
b
c
d

$ cat t.sh
#! /usr/bin/sh

a=`echo "test"` # Execute the command and store the output in a variable

sed  -i "4 i\\$a" t  ## 4 i - is to insert the data in 3rd line ( give appropriately) 

$ cat t
a
b
c
test
d



---------- Post updated at 01:39 AM ---------- Previous update was at 01:27 AM ----------

Sorry, it inserts "test" as a 4th line.

Quote:
Originally Posted by skmdu
Simple way is write a two line shell script.

Something like this,

Code:
$ cat  t
a
b
c
d

$ cat t.sh
#! /usr/bin/sh

a=`echo "test"` # Execute the command and store the output in a variable

sed  -i "4 i\\$a" t  ## 4 i - is to insert the data in 3rd line ( give appropriately) 

$ cat t
a
b
c
test
d

# 4  
Old 11-17-2009
Hi,
Code:
VAR="Hello there"

Try this
Code:
sed "3s/^/$VAR\n/" infile

-or-
Code:
sed "3i$VAR" infile

You need to test if the value of VAR is empty beforehand, otherwise the first command will still insert an empy line, and the second command will fail. If your sed supports inline replacement, you can use sed -i, otherwise you have to do "> newfile" and mv the result to the original file
# 5  
Old 11-17-2009
Quote:
Originally Posted by pluto7777
I need to insert the output of a script into a specific line number of a txt file.
I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this.
An example how to insert file.txt on line 3 of your file:

Code:
awk 'NR==3{system("cat file.txt")}1' file > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

3. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

4. 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

5. Shell Programming and Scripting

Insert output from a file to beginning of line with sed

Hi I've been trying to search but couldn't quite get the answer I was looking for. I have a a file that's like this Time, 9/1/12 0:00, 1033 0:10, 1044 ... 23:50, 1050 How do I make it so the file will be like this? 9/1/12, 0:00, 1033 9/1/12, 0:10, 1044 ... 9/1/12, 23:50, 1050 I... (4 Replies)
Discussion started by: diesel88
4 Replies

6. Shell Programming and Scripting

Insert shell command into first line of output file

How can I insert the command executed on the shell into the first line of my output file? For example if I execute; zcat *.gz |grep “User5501” > users.out How can I make my users.out look like; zcat *.gz |grep “User5501” > users.out User5501 PA User5501 UA User5501 ZA... (3 Replies)
Discussion started by: lewk
3 Replies

7. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies

8. Shell Programming and Scripting

Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />' GREP_FOR='</JOB>' TMP_FILE=/tmp/lineArray.$$ if ]; then continue else ... (7 Replies)
Discussion started by: J-Man
7 Replies

9. UNIX for Dummies Questions & Answers

how to insert a line number on every line

hi... i have a file with data and would like to insert a number and bracket 1) ...2) at the beginning of every successive line; to add some formatting to the text (3 Replies)
Discussion started by: mopimp
3 Replies
Login or Register to Ask a Question