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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract rows from file based on row numbers stored in another file
# 1  
Old 10-05-2011
Extract rows from file based on row numbers stored in another file

Hi All,

I have a file which is like this:

rows.dat

Code:
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):
Code:
1
3
4
5

I want to read numbers.txt file line by line. The extract the row from rows.dat based on the number obtained from numbers.txt, and store in files with names of the rows.

Based on above example, this is what I intend to do:
My first number in numbers.txt is 1, so I create a file 1.num and copy the first row from rows.dat in it.

1.num
Code:
1 2 3 4 5 6

Then I go to second line in numbers.txt and get 3. I create a file 3.num and copy the third row from rows.dat file like this.

3.num
Code:
7 8 9 0 4 3

I keep doing it until the end of numbers.txt file

I have written the code for it, but it is not able to create separate files based on the row number. It copies all rows to another file

Code:
for i in `cat numbers.txt`
do
sed -n "$i p" rows.dat >> op.txt
done

I am doing this in Linux with BASH.
# 2  
Old 10-05-2011
Code:
awk 'NR==FNR{a[FNR]=$0;next} {print a[$1] > $1 ".num"}' rows.dat numbers.txt

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 10-05-2011
If you want to stick to bash script you could do something like:

Code:
while read i
do
     sed -n "$i p" rows.dat > $i.num
done < numbers.txt

However, rdcwayx's solution it more efficient as the above runs sed once for each number in numbers.txt.
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 10-06-2011
Code:
$ awk '{print "sed -n "$0"p rows.dat > "$0".num"}' numbers.txt | sh

This User Gave Thanks to jayan_jay For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

3. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

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

5. Shell Programming and Scripting

Copying down first row in to all the below blank rows in a .csv file

Hi All, I have many of files(.csv) of the format given below. Date,Name,Location 04/02/2012,A,India ,B,China ,C,USA Like this I have 1000's of rows and many columns in all my files. I need a shell script to copy down the Date(in this example column1) to the next 2 rows below(in the... (8 Replies)
Discussion started by: ks_reddy
8 Replies

6. Shell Programming and Scripting

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 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... (2 Replies)
Discussion started by: Muthuraj K
2 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

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies
Login or Register to Ask a Question