Computing.Net > Forums > Programming > awk: search string across 2 files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

awk: search string across 2 files

Reply to Message Icon

Name: koshare
Date: July 16, 2004 at 09:28:15 Pacific
OS: Solaris 8
CPU/Ram: Sparc
Comment:

I have two files of output.

File1 contains:

MachineObjID Name TypeID

File2 contains:

MCID MachineObjID Primary Secondary

I'd like have an awk script that will take $1 in File1, search for this in File2, then insert $2 from File1 into File2. So, the result will look something like:

MCID MachineObjID Name Primary Secondary

I know how to manipulate a single file, but don't know how to take a variable from one file to use in another. Any ideas?

thanks,
Koshare



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: July 16, 2004 at 13:17:24 Pacific
Reply:

I'm not sure about doing this in just AWK by itself. Are you against the idea of a script?

IR


0

Response Number 2
Name: Infinite Recursion
Date: July 16, 2004 at 16:19:00 Pacific
Reply:

Koshare,

I wrote a Awk / Bash script to do what you are wanting to do. There may be a easier way or more streamlined... but this script works. I developed and ran it on a Gentoo
Linux box, so the Solaris platform should not be a problem.

I decided to brush up on my scripting and awk... plus I'm starting a collection of code that I write for people on various forums so I can reference it when others
need the same info.

Here it is, enjoy:

---


#!/bin/bash

# Infinite Recursion
# 7/15/04
# Purpose: Computing.Net Support for user koshare,
# thread 10872

# Reading each line of the first data file
LINES=`cat data.txt`
for i in $LINES
do

# Parsing each line into individual variables for # comparison. Notice the data files must be
# delimited with a ":".

MOID=`echo $i | awk -F":" '{print $1}';`
NAME=`echo $i | awk -F":" '{print $2}';`
TYPEID=`echo $i | awk -F":" '{print $3}';`

# Grab the line from the second data file that contains the
# value that is being searched for from the first data file
# which is $1 or in this case $MOID

LINES2=`cat data2.txt | grep $MOID`

# Parse the line received from the second data file and
# assign the values into variables for use in the final string # construction.

MCID=`echo $LINES2 | awk -F":" '{print $1}';`
MOID=`echo $LINES2 | awk -F":" '{print $2}';`
PRIM=`echo $LINES2 | awk -F":" '{print $3}';`
SEC=`echo $LINES2 | awk -F":" '{print $4}';`

# Sending the new line that contains what the values we
# want into a temporary file

echo $MCID " " $MOID " " $NAME " "
$PRIM " " $SEC >> temp.dat

done


--

The data and result files:

Grendel awk # cat data.txt
Grendel:Apache:Public
Beowulf:FTP:Private

Grendel awk # cat data2.txt
001:Grendel:user1:user2
002:Beowulf:user3:user4

Grendel awk # cat temp.dat
001 Grendel Apache user1 user2
002 Beowulf FTP user3 user4

-----

"cnet_koshare_10872.awk" 34L, 1118C

Hope it helps.

IR


0

Response Number 3
Name: Wolfbone
Date: July 19, 2004 at 04:32:44 Pacific
Reply:

awk 'BEGIN {i=0 ; while (getline < "File1") { l[i]=$0 ; i++ } } ; { for (j=0 ; j<=i ; j++) { split(l[j],f) ; if (f[1] == $2) print $1,$2,f[2],$3,$4} }' File2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: awk: search string across 2 files

How to compare 2 files using awk? www.computing.net/answers/programming/how-to-compare-2-files-using-awk/15598.html

Batch file- read string in txt file www.computing.net/answers/programming/batch-file-read-string-in-txt-file/15532.html

Search Exact string in bat file www.computing.net/answers/programming/search-exact-string-in-bat-file/17923.html