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
# 8  
Old 01-22-2018
Quote:
Originally Posted by Geneanalyst
Thanks everyone,

Just to clarify, if b.txt has 300 lines, then I would like x =300
Note that I corrected the last two awk suggestions in my post #4 after you posted the above (and didn't notice that you had responded until just now). Of course, this is still untested since you haven't supplied any sample data.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 01-22-2018
We are UNIX people separated by different platforms.
This User Gave Thanks to rdrtx1 For This Post:
# 10  
Old 01-22-2018
Quote:
Originally Posted by Don Cragun
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.

Hi Don,

It just occurred to me that x also happens to be the last column of a.txt. Perhaps it would be easier to assign x to be the last column number of a.txt. Any idea how to do that?
# 11  
Old 01-22-2018
Quote:
Originally Posted by Geneanalyst
Hi Don,

It just occurred to me that x also happens to be the last column of a.txt. Perhaps it would be easier to assign x to be the last column number of a.txt. Any idea how to do that?
Very simply:
Code:
awk -F '\t' 'NR > 5 && $NF == "0/0"' a.txt > a1.txt

forget about x and just use the awk NF variable which is the Number of Fields in the current input record.
This User Gave Thanks to Don Cragun For This Post:
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