Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk with two files

Shell Programming and Scripting


Closed    
 
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: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 122 Times in 115 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: 9 September 2015, 11:46 AM EDT
Location: canton, michigan
Posts: 486
Thanks: 0
Thanked 9 Times in 9 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

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 02:47 PM
mget * (obtein files from current directory but not the files form sub-directories) Peter321 Shell Programming and Scripting 0 03-12-2009 12:59 PM



All times are GMT -4. The time now is 08:02 PM.