Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk with two files

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-03-2009
Jonny2Vests Jonny2Vests is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 9 January 2013, 5:19 PM EST
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL awk with two files

I have two files, 1 column each and both are n lines long, eg

-12
-11.836734693877551
-11.673469387755102
...

and

02
04
06
...

I'd like to pattern match line i in file 1, determine its line number and see what the value of the corresponding line in file 2 is.

I can think of various ways of doing it, join etc, but I'd like to use awk for a more elegant solution and one that will help me advance with awk.

I cerrently have (ksh):


Code:
file2val=$(awk -v var=$file1val '$1 ~ var {print NR}' file1 file2)

Which gets me the file1 line number, but I'm not clear on how to do the rest.

Thanks in advance,

Jon
Sponsored Links
    #2  
Old Unix and Linux 12-03-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 118 Times in 111 Posts
Something like that ?

Code:
awk 'NR==FNR{a[NR]=$1;next}{$0= a[FNR] FS $1}1' file2 file1

Sponsored Links
    #3  
Old Unix and Linux 12-03-2009
Jonny2Vests Jonny2Vests is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 9 January 2013, 5:19 PM EST
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
That pastes one file alongside the other. I'd like to pluck a single value from file 2 based on the line number of a pattern match from file1.
    #4  
Old Unix and Linux 12-03-2009
quirkasaurus's Unix or Linux Image
quirkasaurus quirkasaurus is offline Forum Advisor  
Registered User
 
Join Date: Jan 2009
Last Activity: 7 July 2012, 10:53 AM EDT
Location: canton, michigan
Posts: 483
Thanks: 0
Thanked 8 Times in 8 Posts

Code:
nawk 'BEGIN{

  while ((getline x < "a" ) > 0) {
     print x;

     getline y < "b"

    if ( x == "bat" ){
      print y;
      }
    }
  }' /dev/null

infiles:

Code:
==> a <==
ant
bat
cat
dog
emu

==> b <==
ant aardvark
bat basilisk
cat chameleon
dog dugong
emu elephant

output:

Code:
ant
bat
bat basilisk
cat
dog
emu

Sponsored Links
    #5  
Old Unix and Linux 12-03-2009
binlib binlib is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 15 March 2013, 10:40 AM EDT
Location: New Jersey
Posts: 380
Thanks: 7
Thanked 90 Times in 75 Posts
Badly formatted and cryptic since you wanted to advance with awk :-)

Code:
NR==FNR&&f1<1&&$1=="a"{f1=NR}NR>FNR&&f1==FNR

Performance can be improved by adding nextfile (in gawk) or exit.
Sponsored Links
    #6  
Old Unix and Linux 12-03-2009
Jonny2Vests Jonny2Vests is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 9 January 2013, 5:19 PM EST
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Wow, now thats what I call terse. I'll have a play, thanks.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to retrieve all the linked script files/ctl files/sql files? franksubramania Shell Programming and Scripting 1 05-04-2009 01:47 PM
mget * (obtein files from current directory but not the files form sub-directories) Peter321 Shell Programming and Scripting 0 03-12-2009 11:59 AM
text files, ASCII files, binary files and ftp transfers Perderabo Answers to Frequently Asked Questions 0 04-08-2004 04:25 PM



All times are GMT -4. The time now is 05:03 PM.