Insert a variable to a text file after fixed number of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a variable to a text file after fixed number of lines
# 1  
Old 12-29-2011
Insert a variable to a text file after fixed number of lines

Hi,

I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines..

Please help me on this..
Thanks in Advance,
Amrutha
# 2  
Old 12-29-2011
Code:
 
var="ABCD"
#To insert the ABCD after the 3rd line
nawk -v a="$var" '{if(NR==3){print $0;print a}else{print}}' input.txt

---------- Post updated at 02:52 PM ---------- Previous update was at 02:52 PM ----------

Code:
 
bash-3.00$ var="To Insert"
bash-3.00$ cat test.txt
a
b
c
d
e
f
g
bash-3.00$ nawk -v a="$var" '{if(NR==3){print $0;print a}else{print}}' test.txt
a
b
c
To Insert
d
e
f
g

# 3  
Old 12-29-2011
with sed:

$ sed ' 30 a \
New line
' filename
# 4  
Old 12-29-2011
Thanks a lot it works....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Extracting fixed length number from a text file

Hi, I have a text file with sample records as CASE ID: 20170218881083 Original presentment record for ARN not found for Re-presentment I want to extract the 23 digit number from this file. I thought of using grep but initially couldn't extract the required number. However, after... (16 Replies)
Discussion started by: dsid
16 Replies

3. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

4. Shell Programming and Scripting

Count number of free lines in a text file

Hi Everybody I want to write a script to count the number of lines in a file that don't ahve any thing on it, the free lines, i try to do it with fgrep "" which means to grep on the spaces but it does not work. help me please? (3 Replies)
Discussion started by: hard_revenge
3 Replies

5. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

6. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 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

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

10. Shell Programming and Scripting

How to make a number in a text file a variable?

OK this one sounds like it should be a natural... it must be possible to send a number say 200 to a text file and later use it as a variable? yada yada > file.txt how would I retrieve that number to use as a variable? Is this possible? (5 Replies)
Discussion started by: nortypig
5 Replies
Login or Register to Ask a Question