Insert rows based on line numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert rows based on line numbers
# 1  
Old 04-23-2010
Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX

I have a file something like
Code:
A
B
C 
D
E
F

After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I insert a row after C where my reference would be line number 4
# 2  
Old 04-23-2010
Code:
awk 'NR==4 {print "X"
                  print "Y"}
       {print $0} ' inputfile > tmp
mv tmp inputfile

# 3  
Old 04-23-2010
If you want to pass explicitly the line numbers and the values to print

Code:
 
awk -v va1=4 -v pr1="X" -v pr2="Y" 'NR == va1 {print pr1"\n"pr2} {print }' input_file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

2. Shell Programming and Scripting

How to insert random numbers into each line?

I have a file contains thousands of lines. I want to insert n random numbers into each line at specific position. Like this: 0 22…… 1 33…… …… …… I want to insert 3 random numbers from position 2 to 4 into each line. 0 325 22…… 1 685 33…… …… …… Please use CODE tags when... (8 Replies)
Discussion started by: hhdzhu
8 Replies

3. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers based on the last column

I have a tab delimited text file that looks like the following: ERBB3 0.00097 IL31RA 0.000972 SETD5 0.000972 MCART1 0.000973 CENPJ 0.000973 FNDC6 0.000974 I want to assign a number to each row based on the value in the last column (in the order of increasing value so that the first row... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

insert one line inbetween file with use of numbers

Hi All, I have one file name called test.txt and its having numbers inside the file.I need to identified next available number with use of unix shell scipt and have to update next available number with use of unix shell script. Example:- cat test.txt 5001 5002 5003 5005 7000 7001 ... (5 Replies)
Discussion started by: susindram
5 Replies

5. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

6. Shell Programming and Scripting

Extract rows from file based on row numbers stored in another file

Hi All, I have a file which is like this: rows.dat 1 2 3 4 5 6 3 4 5 6 7 8 7 8 9 0 4 3 2 3 4 5 6 7 1 2 3 4 5 6 I have another file with numbers like these (numbers.txt): 1 3 4 5 I want to read numbers.txt file line by line. The extract the row from rows.dat based on the... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

8. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

9. 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
Login or Register to Ask a Question