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:
if the file b.txt contains 3465 lines (AKA records) of text. With that, the command:
would invoke awk with the arguments:
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.
or:
or:
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:
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:
or just set x prior like:
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:
or just set x prior like:
Sorry to disagree with you, but:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)