awk (?) help or just general script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk (?) help or just general script
# 1  
Old 05-25-2009
awk (?) help or just general script

I have two files

(___ represents blanks)

Foo1

1000 345 456
1001 876 908
1002 ___ 786
1003 643 908
1004 345 234

and Foo2

1000 345
1001 876
1002 111
1003 643
1004 345

I would like to read column 2 in foo1 and replace with column 2 (where $1 in foo1 matches $1 in foo2) in foo2 if value is blank.
So output file

1000 345 456
1001 876 908
1002 111 786
1003 643 908
1004 345 234

Thanks

Last edited by garethsays; 05-25-2009 at 02:21 PM..
# 2  
Old 05-25-2009
Code:
awk 'FNR==NR{a[$1]=$2;next}NF <3{print $1,a[$1],$2}NF>2{print}' foo2 foo1


-Devaraj Takhellambam
# 3  
Old 05-25-2009
Quote:
Originally Posted by devtakh
Code:
awk 'FNR==NR{a[$1]=$2;next}NF <3{print $1,a[$1],$2}NF>2{print}' foo2 foo1

-Devaraj Takhellambam
devtakh,

You forgot to check if the first field of Foo1 is an index in the array.

garethsays,

Assuming you have 3 fields in Foo1:

Code:
awk '
NR==FNR{a[$1]=$2;next}
a[$1] && NF==2{$2=a[$1] FS $2}
1' Foo2 Foo1

# 4  
Old 05-25-2009
Alternatively:

Code:
$
$ perl -ne 'BEGIN{open(F,"foo2"); while(<F>){($a,$b)=unpack"a4xa3"; $ids{$a}=$b} close(F)}
> {($x,$y,$z)=unpack("a4xa3xa3"); print "$x $ids{$x} $z\n"}' foo1
1000 345 456
1001 876 908
1002 111 786
1003 643 908
1004 345 234
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

General Purpose Date Script

There must be thousands of one-off solutions scattered around this forum. GNU Date is so handy because it's general but if they're asking they probably don't have it. We have some nice scripts but they tend to need dates formatted in a very particular way. This is a rough approximation which... (18 Replies)
Discussion started by: Corona688
18 Replies

2. UNIX for Dummies Questions & Answers

awk gsub(): general regex

%%%%% (9 Replies)
Discussion started by: lucasvs
9 Replies

3. UNIX for Dummies Questions & Answers

If statements/general help

Dear Unix community over the last couple of days I have been working on learning Unix scripting on iOS using terminal on a jail-broken device... I am having trouble with if statements and giving the user a y or n choice that do different things when entered. The following is my code so far...... (5 Replies)
Discussion started by: mbf123
5 Replies

4. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

5. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

6. UNIX for Dummies Questions & Answers

General Query

Hi all My job is to bootup the servers (1 sun solaris 5.8, 3 sco (release 5) and 1 windows 2000), taking backup of the servers and shut down. Apart from this, there are nearly 150 users for our oracle9i database (forms 3 and d2k as front end). As for as oracle 9i is concerned my job is to... (0 Replies)
Discussion started by: raguramtgr
0 Replies

7. Shell Programming and Scripting

general question?

Perl, Python, and PHP are these languages easy to use? Are they command line or are they part of a GUI? (2 Replies)
Discussion started by: wmosley2
2 Replies

8. UNIX for Dummies Questions & Answers

General Question

Hi, I've been racking my brains trying to remember, but, whats the command to change the default shell? I'm currently always in the Korn shell and I want to start out in the Bash shell. I'm running a variant of BSD I guess in Mac OS X 10.2.2 and Mandrake. Thanks. ccindyderek:confused: (4 Replies)
Discussion started by: ccindyderek
4 Replies
Login or Register to Ask a Question