insert line into file using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert line into file using variable
# 1  
Old 04-06-2009
Error insert line into file using variable

Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5.
So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt.
Now I have problem in inserting the line using the variable $rep. Please help me with awk or sed. Or anyother solution.

lno=$(grep -nw "line5" sample.txt | cut -d: -f1)
grep -n "line3" sample.txt | cut -d: -f1 >out.txt
while read s1
do
if [ $s1 -gt $lno ]; then
rep=$s1 - 1
# awk 'NR == $rep{print "new line"}1' sample.txt >outfile Smilie
sed "/\"rep\"/a \"rep\"x" sample.txt Smilie
break
fi
done <out.txt
---------------------------
Sample.txt
line1
line2
line3
line4
line5
line2
line3
line3
line4
# 2  
Old 04-08-2009
do you mean like this???
Code:
sed '/line5/{
n
/line3/d
a\
new line inserted
}' filename

line1
line2
line3
line4
line5
line2
new line inserted
line3
line3
line4

# 3  
Old 04-12-2009
exactly Vidhayadhar. Thanks
My actual file also has / operator in the inline text as well as in the replacement text. How I can integrate the /. After function x before </head> and </script>

example:
after function (x)

line inserted

</script>

</head>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash: Insert in a variable a file

hi all i have a problem in the bash shell. i'd like insert in a variable a file for example : i have a file datafine.log in this file there is : 17/JUN/2019 i want to insert the value of datafine.log in a variable. Regards Frncesco edit by bakunin: please use CODE-tags for your data... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. Shell Programming and Scripting

Insert value of env variable in xml file

Hello, I have the following variables set in my env echo $MY_XSD_FILE /home/jak/sample.xsd echo $MY_INTERVAL_VALUE 4 I want to insert them between the xml tags in my xml file cat sample.xml ::::::::::::::: ::::::::::::::: <property name="FILE"></property> :::::::::::::::::::::::... (2 Replies)
Discussion started by: jakSun8
2 Replies

5. Shell Programming and Scripting

Insert a new line before every 5th line in a file

Hi, I need to insert a new line containing the string "QUERY" above every 5 lines. The below piece of code inserts a new line after every 5th line awk '{print $0} !(NR%5) {print "QUERY"}' sed 'n;n;n;n;G;' --> I do not know how to give "QUERY" string here But I need to insert it before... (4 Replies)
Discussion started by: royalibrahim
4 Replies

6. Shell Programming and Scripting

script to insert variable file into another, bit like C's #include

Hi, I want to insert one file into another like this: file 1: 1 2 3 <!-- #include file="file2" --> 4 5 <!-- #include file="file3" --> 6 with the complete output going to another file. Have seen some solutions to similar problems using sed, but this problem is different because it is... (5 Replies)
Discussion started by: cvq
5 Replies

7. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

8. Shell Programming and Scripting

Insert text into file depending on variable

Hey guys , i have a variable with the contents ... NUMBER=4 and a test file with the contents 1248 1213 1214 1278 1200 3045 3444 2130 I want to execute a script that will produce the following output ( based on NUMBER=4) to be ... create 1248 (1 Reply)
Discussion started by: theshams
1 Replies

9. Shell Programming and Scripting

insert a variable in the last column of a file

i want to insert a variable in the last column of a file, the columns are separated by "|". I want to insert the variable in everyline of the file . (7 Replies)
Discussion started by: dineshr85
7 Replies

10. UNIX for Advanced & Expert Users

Insert a line as the first line into a very huge file

Hello, I need to insert a line (like a header) as the first line of a very huge file (about 3 ml rows). I am able to do it with sed, but redirecting the output and creating a new file takes quite some time. I was wondering if there was a more efficient way of doing it? Any help would be... (3 Replies)
Discussion started by: shriek
3 Replies
Login or Register to Ask a Question