awk with two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

awk with two files

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 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 117 Times in 110 Posts
Something like that ?

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

Sponsored Links
    #3  
Old 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 12-03-2009
quirkasaurus's Avatar
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 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 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:27 AM.