Sponsored Content
Top Forums Shell Programming and Scripting insert line into file using variable Post 302304333 by aimmanuel on Monday 6th of April 2009 06:58:10 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
std::chrono::duration< _Rep, _Period >(3)		     Library Functions Manual			 std::chrono::duration< _Rep, _Period >(3)

NAME
std::chrono::duration< _Rep, _Period > - SYNOPSIS
Public Types typedef _Period period typedef _Rep rep Public Member Functions duration (const duration &)=default template<typename _Rep2 , typename = typename enable_if<is_convertible<_Rep2, rep>::value && (treat_as_floating_point<rep>::value || !treat_as_floating_point<_Rep2>::value)>::type> constexpr duration (const _Rep2 &__rep) template<typename _Rep2 , typename _Period2 , typename = typename enable_if<treat_as_floating_point<rep>::value || (ratio_divide<_Period2, period>::den == 1 && !treat_as_floating_point<_Rep2>::value)>::type> constexpr duration (const duration< _Rep2, _Period2 > &__d) constexpr rep count () const template<typename _Rep2 = rep> enable_if <!treat_as_floating_point < _Rep2 >::value, duration & > ::type operator%= (const rep &__rhs)" template<typename _Rep2 = rep> enable_if <!treat_as_floating_point < _Rep2 >::value, duration & > ::type operator%= (const duration &__d)" duration & operator*= (const rep &__rhs) constexpr duration operator+ () const duration & operator++ () duration operator++ (int) duration & operator+= (const duration &__d) constexpr duration operator- () const duration & operator-- () duration operator-- (int) duration & operator-= (const duration &__d) duration & operator/= (const rep &__rhs) duration & operator= (const duration &)=default Static Public Member Functions static constexpr duration max () static constexpr duration min () static constexpr duration zero () Detailed Description template<typename _Rep, typename _Period = ratio<1>>struct std::chrono::duration< _Rep, _Period > duration Definition at line 63 of file chrono. Author Generated automatically by Doxygen for libstdc++ from the source code. libstdc++ Tue Jun 10 2014 std::chrono::duration< _Rep, _Period >(3)
All times are GMT -4. The time now is 03:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy