Visit Our UNIX and Linux User Community


Reading specific contents from 1 input files and appending it to another input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading specific contents from 1 input files and appending it to another input file
# 1  
Old 08-21-2008
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  
Old 08-21-2008
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  
Old 08-21-2008
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 04:48 PM..
# 4  
Old 01-13-2009
Try this:

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

Regards
# 5  
Old 01-13-2009
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 06:34 PM..
# 6  
Old 01-14-2009
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

Previous Thread | Next Thread
Test Your Knowledge in Computers #330
Difficulty: Easy
The first dual-core CPU was the Intel Pentium D.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

2. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

3. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

7. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

8. Shell Programming and Scripting

Reading input files

Okay, so I've looked on here and found some similar things, but not exactly what I am looking for. I am working on creating a script that can back up some files, based on the contents of another file - the configuration file. First file contains the files to back up - we'll call this... (1 Reply)
Discussion started by: pdxwarrior
1 Replies

9. Shell Programming and Scripting

awk reading 2 input files but not getting expected value

I'm reading 2 input files but not getting expected value. I should get an alpha value on file_1_data but not getting any. Please help. >cat test6.sh awk ' FILENAME==ARGV { file_1_data=$0; print "----- 1 Line " NR " -----" $1; next } FILENAME==ARGV { file_2_data=$0; print "----- 2... (1 Reply)
Discussion started by: pdtak
1 Replies

10. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies

Featured Tech Videos