Reading and copying a selected rows


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Reading and copying a selected rows
# 1  
Old 08-13-2016
Reading and copying a selected rows

Dear All,

I have a data file input.res like below. (Only six column shown here for example.)
Sequence of first column starting from 1 to 148.

Input file
Code:
1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0
1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0
1 Q0 3_April_2014_Entertainment4 2 19.99980848390178 BM25b1.0
1 Q0 13_December_2012_Page324 3 19.49552258200046 BM25b1.0
1 Q0 18_December_2012_Page331 4 19.379322689633636 BM25b1.0
1 Q0 9_February_2013_International1 5 19.37324470193972 BM25b1.0
2 Q0 31_October_2012_MuslimWorld9 0 16.50230172618038 BM25b1.0
2 Q0 31_October_2012_ 0 1 16.244196279676054 BM25b1.0
2 Q0 16_November_2012_ 12 2 16.204816883686515 BM25b1.0
2 Q0 31_October_2012_ 1 3 15.947697590184493 BM25b1.0
2 Q0 31_October_2012_MuslimWorld8 4 15.811735282661466 BM25b1.0
2 Q0 9_November_2012_LocalNews5 5 14.906807130618539 BM25b1.0
...........
...........
...........
148 Q0 3_January_2012_Page39 0 13.227415420268592 BM25b1.0
148 Q0 6_March_2012_Front1 1 13.023359161460377 BM25b1.0
148 Q0 3_June_2014_Front13 2 12.969833413025505 BM25b1.0
148 Q0 27_March_2012_Page36 3 12.687908813980718 BM25b1.0
148 Q0 11_March_2013_Front10 4 12.668886361211987 BM25b1.0
148 Q0 29_June_2014_Page30 5 12.607198882770502 BM25b1.0

I want to select first four lines for each number and copying them to a seperate new files.

file1
Code:
1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0
1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0
1 Q0 3_April_2014_Entertainment4 2 19.99980848390178 BM25b1.0
1 Q0 13_December_2012_Page324 3 19.49552258200046 BM25b1.0

file2
Code:
2 Q0 31_October_2012_MuslimWorld9 0 16.50230172618038 BM25b1.0
2 Q0 31_October_2012_ 0 1 16.244196279676054 BM25b1.0
2 Q0 16_November_2012_ 12 2 16.204816883686515 BM25b1.0
2 Q0 31_October_2012_ 1 3 15.947697590184493 BM25b1.0

........
........

file148
Code:
148 Q0 3_January_2012_Page39 0 13.227415420268592 BM25b1.0
148 Q0 6_March_2012_Front1 1 13.023359161460377 BM25b1.0
148 Q0 3_June_2014_Front13 2 12.969833413025505 BM25b1.0
148 Q0 27_March_2012_Page36 3 12.687908813980718 BM25b1.0

thanks in advance


Moderator's Comments:
Mod Comment Pease use code tags instead of icode tags (the button called "code")

Last edited by Scrutinizer; 08-13-2016 at 02:44 AM.. Reason: Changed code tags to code tags
# 2  
Old 08-13-2016
Try something like this:
Code:
awk '{f="file" $1} p!=f{if(p)close(f); p=f} ++C[$1]<=4{print >f}' file

The input file needs to grouped on field 1 for this to work. If that is not the case, it needs to be sorted first..
Closing the files is necessary, otherwise awk will have too many files open at once...

Last edited by Scrutinizer; 08-13-2016 at 02:57 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-13-2016
Hello mranrasheedamu,

Could you please try following and let me know if this helps you.
Code:
 awk '{if(++Q[$1]<5){print $0 >> "file"$1} else {close("file"$1)}}'  Input_file

Output it created 2 files named file1 and file2, because have put lines into Input_file only till 2 number only.

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

2. Shell Programming and Scripting

Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file. Any help would be great Thanks (6 Replies)
Discussion started by: kylle345
6 Replies

3. Shell Programming and Scripting

Reading selected lines from a param file

Hi all, I have a question for the Gurus. I apologize if this has bee shared before but I couldn't find the link. I am trying to read parameters from an external parameter file. What I m trying to achieve is read selected lines from an external parameter file into the script. for eg my param... (4 Replies)
Discussion started by: maverick1947
4 Replies

4. Shell Programming and Scripting

To extract selected rows

Hi, I have two files Input1.txt and Input2.txt and i need to have a output with selected lines starting with the values present in Input2.txt. For example: Input1.txt:... (5 Replies)
Discussion started by: bhas
5 Replies

5. UNIX for Dummies Questions & Answers

Remove a newline char from selected rows.

Greetings! Can we automate the process of removing a newline char from selected rows in a fixed width file using a shell? Input is like abcd1234 xyzd1234 abcd a1b2c3d4 abcd1234 xyzd1234 xx abcd1234 Expected output - abcd1234xyzd1234 abcda1b2c3d4abcd1234xyzd1234 xxabcd1234 ... (2 Replies)
Discussion started by: mailme0205
2 Replies

6. Shell Programming and Scripting

copying selected records from two columns to another file

Hey guys I have got a tab-separated file and I want to copy only selected records from two columns at a time satisfying specified condition, and create a new file. My tab separated file is like this ID score ID score ID Score ID score ID score 1_11 0.80 2_23 0.74 2.36 0.78 2_34 0.75 A_34... (9 Replies)
Discussion started by: jacks
9 Replies

7. Shell Programming and Scripting

print selected rows with awk

Hi everybody: Could anybody tell me how I can print from a file a selected rows with awk. In my case I only want print in another file all the rows from NR=8 to NR=2459 and the increment each 8 times. I tried to this: awk '{for (i=8; i=2459; i+=8); NR==i}' file1 > file2 But doesn't... (6 Replies)
Discussion started by: tonet
6 Replies

8. Shell Programming and Scripting

return number of rows selected

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried $selectedrows = $sth->numrows; i got the error msg: Can't locate object method "numrows" via package "DBI::st" i changed it to $selectedrows =... (7 Replies)
Discussion started by: new2ss
7 Replies

9. Shell Programming and Scripting

Copying selected content from file

I want to capture contents of a file between 2 strings into another file for eg all lines in between the keywords "start log" and "end log" should be copied into another file (1 Reply)
Discussion started by: misenkiser
1 Replies

10. Shell Programming and Scripting

Cutting rows after a selected point

I have a log file from which I want to cut out the ten lines assoictiated to my search requirment (cust_ref #). The cust_ref number will be on te first line and the update information will be on the following ten lines (which dosen't linking data to grep on). Using a script I would like to... (4 Replies)
Discussion started by: nhatch
4 Replies
Login or Register to Ask a Question