0

Basically, I have two files

dupestest.txt

152,153
192,193
215,216
290,291
2279,2280
2282,2283

haftest.txt

152,ABBOTS ROAD
153,ABBOTS ROAD
154,ABBOTS ROAD
155,ABBOTS ROAD
156,ABBOTS ROAD
157,ABBOTS ROAD

I want to find the numbers in dupestest.txt in haftest.txt and produce this outcome: results.txt

152,ABBOTS ROAD,153 ABBOTS ROAD 
192,ABBOTS ROAD,193,ABBOTS ROAD

etc

Can anyone give me any advice?

It has to be in awk.

6
  • 1
    Have you tried anything? Commented May 29, 2013 at 10:01
  • I have just been looking online for answers, I am very new to AWK and I have seen alot of command line answers but I want it in a script Commented May 29, 2013 at 10:05
  • Basically, the command line and a script is nearly equal. You can just put the command line into a file and try to run it. Commented May 29, 2013 at 10:07
  • But I have yet to find a command line that works, thats why I am asking here for some advice Commented May 29, 2013 at 10:08
  • In your real data is $2 always the same in haftest? for example would you have different values for 152 and 153? Commented May 29, 2013 at 13:01

2 Answers 2

3

Try this:

awk -F, 'BEGIN{OFS=","} FNR==NR{a[$1]=$2; next} $1 in a || $2 in a{print $1, a[$1], $2, a[$2]}' haftest.txt dupestest.txt

This line of script tests if either the first or the second key in dupestest.txt exists in haftest.txt, and prints its associated value in dupestest.txt if one of the keys exists, you may have to tweak the script a little bit if only one of the two keys exists in haftest.txt to get the desired output, that's left as an exercise for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice. I was wondering how to get rid of the spaces in output with print - you did that with OFS=","
@user2428639, this script runs perfectly here with your supplied sample data. Well, I will not help you further, because I don't see that you are trying yourself, I don't even see any useful error messages when you say giving parse error, now you are on your own.
0

try this

awk -F, 'NR==FNR{a[$1]=$0;next}$1 in a&&$2 in a{print a[$1]","a[$2]}' haft.txt dup.txt

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.