To split a file in exact half using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To split a file in exact half using awk
# 1  
Old 07-20-2014
To split a file in exact half using awk

file:
Code:
1
2
3
4
5
6
7
8
9
10

code:
Code:
awk 'END{if( < NR/2) {print $0}}' file

The above has some error

Output needed:
Code:
1
2
3
4
5

Can some one help me to get the above output.
# 2  
Old 07-20-2014
Try:
Code:
awk '{x[++xc] = $0}END{for(i = 1; i <= xc/2; i++) print x[i]}' file

but, of course, this won't print a half line if the number of input lines is odd.

Last edited by Don Cragun; 07-20-2014 at 04:17 PM.. Reason: Fix condition...
# 3  
Old 07-20-2014
Roozo, your condition
Code:
if( < NR/2)

is incomplete.

Try this:
Code:
awk -v totrecs=$(wc -l < file) ' {if (NR <= totrecs/2) {print $0}}' file
1
2
3
4
5

# 4  
Old 07-20-2014
Hi, another awk solution (tested with gnu awk):
Code:
awk -F"\n" -vOFS="\n" '{X=X$0"\n"}END{$0=X;NF=NR/2;print}' file

This User Gave Thanks to disedorgue For This Post:
# 5  
Old 07-20-2014
Although this version reads the file twice it could turn out to be more efficient when dealing with large files or limited resource systems, it doesn't involve copying large amounts of data into memory.

Code:
awk 'FNR==NR{lines++;next}FNR<=lines/2' file file

# 6  
Old 07-20-2014
Good point Chubler_XL: And, if the file is relatively large, you can speed it up by only reading the file one and a half times instead of twice:
Code:
awk 'FNR==NR{lines++;next}FNR>lines/2{exit}1' file file

With a ten line file, my original proposal is probably faster. With a 1Gb file, this will consume much less memory while it is running. With a 1Tb file, awk may not be able to allocate enough resources to do the job the 1st way while this way should work just fine.

Only the original poster knows how much data is being processed, how much memory and swap space are available, what type of processor, ... and can choose what will work best in his or her environment.
# 7  
Old 07-20-2014
Middle ground, using half the memory, reading file once, without an END section:
Code:
awk '{i=NR/2} i in A{print A[i]; delete A[i]}{A[NR]=$0}' file

or
Code:
awk '{A[NR]=$0} !(NR%2){print A[NR/2]; delete A[NR/2]}' file


---
Probably this is the most efficient :
Code:
awk -v h=$(echo $(wc -l <file)) 'NR>h/2{exit}1' file


Last edited by Scrutinizer; 07-20-2014 at 09:30 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split file using awk

I need to split the incoming source file in to multiple files using awk. Split position is (6,13) : 8 positions All the records that are greater than 20170101 and less than or equal to 20181231 should go in a split file with file name as source... (11 Replies)
Discussion started by: rosebud123
11 Replies

2. Solaris

Steps to reestablish SRDF which is half split

HI Guys, Can you please let me know the procedure to reestablish the SRDF which is half split, as you can see from the below O/P that one of the device is synchronized and other devices are in split mode Source (R1) View Target (R2) View MODES... (2 Replies)
Discussion started by: ravijanjanam12
2 Replies

3. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

4. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

5. Shell Programming and Scripting

Split a file with awk

Hi! I have a file like this: a,b,c,12,d,e a,b,c,13,d,e a,b,c,14,d,e a,b,c,15,d,e a,b,c,16,d,e a,b,c,17,d,e I need to split that file in two: If field 4 is equal or higher than 14 that row goes to one file and if it is equal or higher than 15 to another. Can anyone point me in the... (2 Replies)
Discussion started by: Tr0cken
2 Replies

6. Shell Programming and Scripting

awk - split file

How can I split a text file (in awk) in n others with number of record given in input? Thanks (6 Replies)
Discussion started by: pinguc
6 Replies

7. Shell Programming and Scripting

split file with awk

I did a lot of search on this forum on spiting file; found a lot, but my requirement is a bit different, please guide. Master file: x:start:5 line1:23 line2:12 2:90 x:end:5 x:start:2 45:56 22:90 x:end:2 x:start:3 line1:23 line2:12 x:end:3 x:start:2 line5:23 (1 Reply)
Discussion started by: uwork72
1 Replies

8. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

9. Shell Programming and Scripting

Split file using awk

I am trying to read a file and split the file into multiple files. I need to create new files with different set of lines from the original file. ie, the first output file may contain 10 lines and the second 100 lines and so on. The criteria is to get the lines between two lines starting with some... (8 Replies)
Discussion started by: pvar
8 Replies
Login or Register to Ask a Question