The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk reading 2 input files but not getting expected value pdtak Shell Programming and Scripting 1 04-29-2008 10:03 PM
Help reading an input file in KSH zilla30066 Shell Programming and Scripting 2 02-01-2007 06:45 AM
Script for reading an input file gzs553 Shell Programming and Scripting 1 10-17-2006 06:55 AM
Reading Input from File and Duplicates Output noelcantona Shell Programming and Scripting 6 10-18-2005 04:59 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-21-2008
sksahu sksahu is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Reading specific contents from 1 input files and appending it to another input file

Hi guys,

I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help.

I have 2 input files (example given below)

Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue). The content of first input file Input file 1 will keep changing though.

If we get a match with first number [before first dot (.)] then concatenate the name from second file at the end with a comma (,).

Input file 1
Aug 14 10:37 123.XYZ.106.20061023160210.in
Aug 14 12:59 135.XYZ.107.20061023160210.in
Aug 14 13:15 234.XYZ.108.20061023160210.in

Input file 2
111,ABCD
123,EFGH
124,TYUI
125,RTYUGH
135,RTYT
139,REFG
234,GHJK
345,GHJK
346,JKHG

Output file
Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH
Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT
Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK

Appreciate your responses.
  #2 (permalink)  
Old 08-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$4 in a{print $0 "," a[$4]}
' file2 file1
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #3 (permalink)  
Old 08-21-2008
sksahu sksahu is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Franklin52 - Thanks for your help. One more question though, If I have another input file

Input file 3
XYZ,Desc1
VZG,Desc2

And if I would like to add more to output file by looking into input file 3, then how would I get the "XYZ" to get the description from input file 3.

Output file
Desc1,Aug 14 10:37 123.XYZ.106.20061023160210.in, EFGH
Desc1,Aug 14 12:59 135.XYZ.107.20061023160210.in, RTYT
Desc1,Aug 14 13:15 234.XYZ.108.20061023160210.in, GHJK

I tried this but no luck

awk -F",| |\\\.\." 'NR==FNR{a[$1]=$2;next}
$4 in a{print $0 "," a[$4]}
' file3 file1

Appreciate your responses.

Last edited by sksahu; 01-13-2009 at 03:48 PM..
  #4 (permalink)  
Old 01-13-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$5 in a{print a[$5] "," $0}' file3 file1
Regards
  #5 (permalink)  
Old 01-13-2009
sksahu sksahu is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
No it didn't work. Let me tell you what is the scenario...

I have 3 input files (example given below)

Input file 2 & 3 are a standard file (it will not change) and we have to get the name (second column after comma) from it and append it in the Input file 1 (see output file below highlighted in blue & magenta). The content of first input file Input file 1 will keep changing though.

If we get a match with first number [before first dot (.)] then concatenate the name from second file at the start with a comma (,).

If we get a match with first string [after first dot (.)] then concatenate the name from third file at the start with a comma (,).

Input file 1
123.XYZ.106.20061023160210.in
135.XYZ.107.20061023160210.in
234.XYZ.108.20061023160210.in

Input file 2
111,ABCD
123,EFGH
124,TYUI
125,RTYUGH
135,RTYT
139,REFG
234,GHJK
345,GHJK
346,JKHG

Input file 3
XYZ,Desc1
VZG,Desc2

Output file
EFGH,Desc1,123.XYZ.106.20061023160210.in
RTYT,Desc1,135.XYZ.107.20061023160210.in
GHJK,Desc1,234.XYZ.108.20061023160210.in

I got the first part as following :-
awk -F",| |\\\." 'NR==FNR{a[$1]=$2;next}
$1 in a{print a[$1] "," $0}
' file2 file1 >> output file

But I am not getting the second part.

Appreciate you response.

Last edited by sksahu; 01-13-2009 at 05:34 PM..
  #6 (permalink)  
Old 01-14-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk -F",|\\\." '
FILENAME=="file3" {a[$1]=$2;next}
FILENAME=="file2" {b[$1]=$2;next}
a[$2] && b[$1] {$0=b[$1]"," a[$2]"," $0}
{print}' file3 file2 file1
Regards
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, file manipulation, unix

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0