Assign number of records to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign number of records to a variable
# 1  
Old 01-22-2018
Question Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file.

I have a simple command such as below:

Code:
awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt


but I want x to equal the number of records in a different file, b.txt
# 2  
Old 01-22-2018
How do you define and determine the "number of records in a different file"? How can that number equal "0/0"

Last edited by RudiC; 01-22-2018 at 12:02 PM..
# 3  
Old 01-22-2018
try something like:
Code:
awk -F "\t" 'NR>5 {if (x==8) print $0}' x=$(wc -l b.txt) a.txt

Note: x will be a number
# 4  
Old 01-22-2018
Come on RudiC and rdrtx1,
In awk $x is the contents of a field in a line and that field might or might not be a number.

Note that the output of the command wc -l b.txt) will be something like:
Code:
    3465 b.txt

if the file b.txt contains 3465 lines (AKA records) of text. With that, the command:
Code:
awk -F "\t" 'NR>5 {if (x==8) print $0}' x=$(wc -l b.txt) a.txt

would invoke awk with the arguments:
Code:
awk -F "\t" 'NR>5 {if (x==8) print $0}' x= 3465 b.txt a.txt

Shell variable assignments only happen in shell command line processing when they occur at the start of a command line. In a shell variable assignment, the leading spaces in the command substitution would be assigned to the variable, but when it appears as a parameter on a command-line after the utility name, the spaces before and after the line count in the output from wc act as field separators.

Hi Geneanalyst,
One might guess that one of the following might do what you requested.
Code:
awk -F '\t' -v x=$(( $(wc -l < b.txt) + 0)) '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt

or:
Code:
awk -F '\t' '
FNR == NR {
	x++
	next
}
(NR + x) > 5 {
	if(($x == "0/0")) { print }
}' b.txt a.txt > a1.txt

or:
Code:
awk -F '\t' '
FNR == NR {
	x++
	next
}
(NR + x) > 5 && $x == "0/0"' b.txt a.txt > a1.txt

One might also guess that you don't really want the awk variable x to be the number of records in b.txt, but instead want it to be the number of fields in that file. If that is what you want, it is even more important than in many other cases to know what operating system and shell you're using because some versions of awk have a nextfile command and others don't.

And, as always, sample input files and desired output would help us help you.

Last edited by Don Cragun; 01-22-2018 at 12:58 PM.. Reason: Fix description of what happens with an unquoted x=$(wc -l b.txt) after a command name. And fix line number calculations.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-22-2018
Thanks everyone,

Just to clarify, if b.txt has 300 lines, then I would like x =300


EDIT: Also, in some cases I would like to set x=number of lines in b.txt + 9

Last edited by Geneanalyst; 01-22-2018 at 01:14 PM.. Reason: see above
# 6  
Old 01-22-2018
Wrongo. In the above x=$(wc -l b.txt) will set x to the line count and b.txt will be read by the script. Not sure what the intent of the script is and if the reading of b.txt is needed. So if b.txt is not needed to be read and just the line count is needed then try something like:
Code:
awk '{print $0, x}' $(wc -l b.txt| read x y; echo x=$x) infile

or just set x prior like:
Code:
wc -l b.txt | read x y
awk '{print $0, x}' x=$x a.txt


Last edited by rdrtx1; 01-22-2018 at 01:00 PM..
# 7  
Old 01-22-2018
Quote:
Originally Posted by rdrtx1
Wrongo. In the above x=$(wc -l b.txt) will set x to the line count and b.txt will be read by the script. Not sure what the intent of the script is and if the reading of b.txt is needed. So if b.txt is not needed to be read and just the line count is needed then try something like:
Code:
awk '{print $0, x}' $(wc -l b.txt| read x y; echo x=$x) infile

or just set x prior like:
Code:
wc -l b.txt | read x y
awk '{print $0, x}' x=$x a.txt

Sorry to disagree with you, but:
Code:
set -xv
awk -F'\t' '{print x}' x=$(wc -l b.txt) a.txt
+ wc -l b.txt
+ awk '-F\t '{print x} x= 3466 b.txt a.txt
awk: can't open file 3466
 source line number 1

As I said before, x is set to the empty string, the number of lines counted by wc and the file processed by wc both become separate arguments to awk.

This was tested with both bash and ksh on macOS High Sierra version 10.13.2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Assign large number of blanks to a variable

I want to assign large number of blanks to a variable in Korn shell. If it is a small number it is fine like if I want to assign 3 blanks I would code var=" " But if it is a big number say 100 blanks, what is a better way? Ultimately I will use it in printf statement printf... (3 Replies)
Discussion started by: Soham
3 Replies

3. Shell Programming and Scripting

Use GREP to count number of records and place it in a variable

I am trying to count the number of records from different files using grep, and then place the result in a separate variable for each file, so at the end of my shell script, I can sum all the variables and check if the number of records are equal to what I was expecting. It is weird butwc -ldoes... (2 Replies)
Discussion started by: dhruuv369
2 Replies

4. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

5. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

6. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

7. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

8. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question