Visit Our UNIX and Linux User Community


Usage of Int with NR in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Usage of Int with NR in awk
# 1  
Old 08-08-2014
Question Usage of Int with NR in awk

Hello Everyone,

I am new to awk and trying my hand with the diff codes and came across the below code today. It would be great if any of the Guru's help me to understand.

Code:
awk '{filename = "sample_file" int((NR-1)/34) ".DAT"; print >> filename}' sample_file.DAT

34 is the no of lines each file should be splitted into. The above command is writted to splie the file sample_file.DAT to have 34 lines each. Can somebody tell me how these int ((NR-1)/34) works. I blieve NR is nothing but the no of lines in the input file.

Last edited by saratha14; 08-08-2014 at 10:58 AM.. Reason: typo
# 2  
Old 08-08-2014
Try yourself. Run:
Code:
awk 'BEGIN {for (i=1; i<=69; i++) print i, "filename" int((i-1)/34)".DAT"}'

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-08-2014
Hi RudiC,
Got it.so NR is nothing but the no of lines read by the awk and the for loop printed me 1 till 69.
# 4  
Old 08-08-2014
NR is you the total number of records being processed or line number
# 5  
Old 08-08-2014
Yeah.
Code:
awk '{filename = "sample_file" int((NR-1)/34) ".DAT"; print >> filename}' sample_file.DAT

In that code, so this is how I understand,
first iteration , it reads till 34th line 34-1/34 and it will put 0 and the file name will be sample_file0.DAT and then it will print till that line into that file sample_file0.DAT... the second iteration it will be 68-1/34 and it will round the integer to 1 and the file name will be sample_file1.DAT and so on.. Is it correct.?
# 6  
Old 08-08-2014
More or less correct, but int() doesn't round, it truncates, throwing away everything after the decimal point whether it be it #.0000000000034 or #.9999999999999999977.

NR is the number of records. awk uses \n to separate records by default, but can use anything you tell it to. It can be useful to set it to < to parse XML files for example.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-08-2014
Yes Corona688. You are correct int truncate. This is the reason I get 0 ,1 .. so on.. But I have one more doubt, how does awk know that it has to print the first 34 lines to first set of file and so on..

Previous Thread | Next Thread
Test Your Knowledge in Computers #1000
Difficulty: Medium
Trusted Computer System Evaluation Criteria (TCSEC) is frequently referred to as the Red Book.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk usage

Hi I have a file like this user="joe" phone="4280919" duration=128 login=12/02/2016 ip-address=10.230.120.35 user="mary" phone="6352728" duration=23 login=16/02/2016 ip-address=10.123.231.54 I have to convert these to as follows (csv format) (Ignore Login field)... (6 Replies)
Discussion started by: Balav
6 Replies

2. Shell Programming and Scripting

Help with awk - Disk usage

Hi All, Would appreciate your help as to why the following code not showing the correct output. the issue is on the last else cause. I am trying to report on disk space based on percentage usage. for some reason, it's showing output as OK even thou disk space is 90% !!!! any ideas why? thanks!!... (2 Replies)
Discussion started by: xcod3r
2 Replies

3. Shell Programming and Scripting

Advanced AWK Regexp substring to int & Replace

Hi! I have a difficult problem, to step up a unknown version number in a text file, and save the file. It would be great to run script.sh and the version gets increased. Example the content of the textfile.txt hello version = x bye This include three steps 1. First find the char after... (2 Replies)
Discussion started by: Beachboy72
2 Replies

4. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

5. Shell Programming and Scripting

Awk issue using int function

Hi, I am having issue with awk command . This command is running in the command prompt but inside a shell script. awk -F'| ' 'int($1)==$1 && int($3) ==$3' int_check.txt $cat int_check.txt 123|abc|123x 234|def|345 When i run it inside a shell script i am getting the error "bailing... (5 Replies)
Discussion started by: ashwin3086
5 Replies

6. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

7. Shell Programming and Scripting

problem in awk int() function

awk -vwgt=$vWeight -vfac=$vFactor ' BEGIN { printf("wgt:" wgt "\n"); printf("factor:" fac "\n"); total = sprintf("%.0f", wgt * fac); total2 = sprintf("%.0f", int(wgt * fac)); printf("total:" total "\n"); printf("total2:" total2 "\n"); } ' if vWeight=326.4 vFactor=100 the result... (2 Replies)
Discussion started by: qa.bingo
2 Replies

8. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

9. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

10. Programming

difference between int ** func() and int *& func()

What is the difference between int** func() and int*& func(). Can you please explain it with suitable example. Thanks, Devesh. (1 Reply)
Discussion started by: devesh
1 Replies

Featured Tech Videos