Passing specific fields from files as variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Passing specific fields from files as variables
# 1  
Old 04-13-2005
Passing specific fields from files as variables

Hi all,

I have a question regarding passing specific fields from files to shell scripts as variables. I have 2 files, I want to compare the record counts of these files in a shell script. Thus far I have done:

wc -l file1 > file1test

yields: 73 filename

wc -l file2 > file2test
yields: 73 filename

What I want to do is take the 73 field from each file and store them in separate variables, then use those variables in a conditional statement.

Any help would be greatly appreciated.

Last edited by keladar; 04-14-2005 at 01:04 AM..
# 2  
Old 04-13-2005
Quote:
Originally Posted by keladar
I have a question regarding passing specific fields from files to shell scripts as variables. I have 2 files, I want to compare the record counts of these files in a shell script. Thus far I have done:

wc -l file1 > file1test

yields: 73 filename

wc -l file2 > file2test
yields: 73 filename

What I want to do is take the 73 field from each file and store them in separate variables, they use those variables in a conditional statement.

Code:
VARIABLE_1=`wc -l file1 | awk -F\  '{print $1}'`
VARIABLE_2=`wc -l file2 | awk -F\  '{print $1}'`

# 3  
Old 04-13-2005
or ...
Code:
VARIABLE_1=`awk 'END {print NR}' file1`
VARIABLE_2=`awk 'END {print NR}' file2`

# 4  
Old 04-13-2005
Quote:
Originally Posted by Just Ice
or ...
Code:
VARIABLE_1=`awk 'END {print NR}' file1`
VARIABLE_2=`awk 'END {print NR}' file2`

Now you made me wonder if one was faster.
Code:
$ time awk 'END {print NR}' bigfile
1314268

real    1m50.36s
user    1m42.52s
sys     0m5.35s

$ time wc -l bigfile | awk -F\  '{print $1}'
1314268

real    0m18.67s
user    0m13.19s
sys     0m4.33s

# 5  
Old 04-13-2005
All,

Thanks alot, this worked well. I appreciate the help!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. UNIX for Dummies Questions & Answers

Passing variables to mkuser

In a ksh, I'm attempting to pass my string of arguments to the mkuser command in a variable as follows... cmd="pgrp=ACRGENU groups=ACRGENU home=/home/${USERID} shell=/usr/bin/ksh" cmd=$cmd" gecos='${USERNAME}' login=true su=false rlogin=true daemon=true" cmd=$cmd" admin=false... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Passing Variables

I have below data: DAY1=10202013 I am trying below but not getting the desired output: COUNT=1 DATE=DAY$COUNT echo "Date is $DATE" However output I am getting is: Date is DAY1 I wanted the output as: Date is 10202013 I tried following as well: DAY1=10202013 COUNT=1... (3 Replies)
Discussion started by: rockyr1985
3 Replies

5. UNIX for Dummies Questions & Answers

Set variables from fields in fields

Hi, This is my first post here and I am a newbie. :) I have a file that looks like this : Introduction:Intro_123.html Product definition:Prod_def.html System Setup:SSetup-64bit.html Setting up user accounts:Set_user_acc.html I tried to create a script that would output "The filename... (3 Replies)
Discussion started by: Joq
3 Replies

6. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. UNIX for Dummies Questions & Answers

Passing variables between scripts...

Hey all, I'm wondering how you pass variable's that are defined in one script to another script that's been called by that first script..... Best regards, Jaz (1 Reply)
Discussion started by: Jazmania
1 Replies

9. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies

10. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies
Login or Register to Ask a Question