Sponsored Content
Full Discussion: awk with two files
Top Forums Shell Programming and Scripting awk with two files Post 302377189 by Jonny2Vests on Thursday 3rd of December 2009 10:21:37 AM
Old 12-03-2009
MySQL awk with two files

I have two files, 1 column each and both are n lines long, eg

-12
-11.836734693877551
-11.673469387755102
...

and

02
04
06
...

I'd like to pattern match line i in file 1, determine its line number and see what the value of the corresponding line in file 2 is.

I can think of various ways of doing it, join etc, but I'd like to use awk for a more elegant solution and one that will help me advance with awk.

I cerrently have (ksh):

Code:
file2val=$(awk -v var=$file1val '$1 ~ var {print NR}' file1 file2)

Which gets me the file1 line number, but I'm not clear on how to do the rest.

Thanks in advance,

Jon
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting input files into multiple files through AWK command

Hi, I needs to split *.txt files from single directory depends on the some mutltiple input values. i have wrote the code like below for file in *.txt do grep -i -h "value1|value2" $file > $file; done. My requirment is more input values needs to be given in grep; let us say 50... (3 Replies)
Discussion started by: arund_01
3 Replies

2. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

3. UNIX for Dummies Questions & Answers

Using Awk within awk to read all files in directory

I am wondering if anyone has any idea how to use an awk within awk to read files and find a match which adds to count. Say I am searching how many times the word crap appears in each files within a directory. How would i do that from the command prompt ... thanks (6 Replies)
Discussion started by: flevongo
6 Replies

4. Shell Programming and Scripting

Awk : too many files 10

Hi, I m trying to split the one file into small segments based on userid field.I m getting the error "AWK : too many output files 10 record number 71". Can any one help me to resolve this issue or provide me someother code? Please note that, I don't have root admin previlege. I m login... (12 Replies)
Discussion started by: prabuk1
12 Replies

5. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

6. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

7. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

8. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

9. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

10. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies
join(1) 						      General Commands Manual							   join(1)

Name
       join - join files

Syntax
       join [ -a n] [ -e string] [ -j  n m] [ -o list] [ -t c]	file1 file2

Description
       The  command  compares a field in file1 to a field in file2.  If the two fields match, the command combines the line in file1 that contains
       the field with the line in file2 that contains the field.  The command writes its output to standard output.  If you specify a  hyphen  (-)
       in the file1 argument, compares standard input to the contents of file2.

       The command compares and combines the input files one line at a time. Each line in the input file contains one field that uses to determine
       if two lines should be joined.  This field is called the join field. By default, the command uses the first field in each line as the  join
       field.	The  command  compares	the join field in the first line of file1 to the join field in the first line of file2.  If the two fields
       match, the command joins the lines.  The command then compares the join fields in the second line of both files, and so on.

       In the input files, fields are separated by tab or space characters.  The command reads data from the first field until it encounters a tab
       or  space  character,  which  terminates the first field.   By default, the command ignores tab and space characters, so the next character
       that is not a tab or space begins the second field.  The second field is terminated by the tab or space that  follows  it,  and	the  third
       field begins with the next character that is not a tab or space.  The command reads fields in this way until it encounters a new line char-
       acter.  Any number of tabs or spaces can separate two fields, and any number of newline characters can separate two lines.

       Both file1 and file2 must be ordered in the collating sequence of the command on the fields that  the  two  files  are  to  be  joined.	By
       default, uses the first field in each line and collates the same as

       To  create  output,  the  command writes the join field, followed by the remaining fields in the line from file1, followed by the remaining
       fields in the line from file2 to the output file.  The following demonstrates how lines in the  output appear by default:
       join_field file1.field2 file1.field3 file1.field4 file2.field2 file2.field3

       By default, the command ignores lines that do not contain identical join fields.  The command writes no output for these lines.

       You can change how creates output using command options.  For example, you can cause the command to write output for lines that do not con-
       tain  identical	join  fields.	You  can  also	specify  a  list  using  the option.  In list, you supply a list of specifiers in the form
       file.field, where file is either 1 or 2 and field is the number of the field.  For example, 1.2 specifies the second  field  in	the  first
       file  and 2.4 specifies the fourth field in the second file. The following demonstrates how lines in the output appear if you use these two
       specifiers:
       file1.field2 field2.field4

   International Environment
       LC_COLLATE     If this environment variable is set and valid, uses the international language database named in the definition to determine
		      collation rules.

       LC_CTYPE       If this environment variable is set and valid, uses the international language database named in the definition to determine
		      character classification rules.

       LANG	      If this environment variable is set and valid uses the international language database named in the definition to  determine
		      collation  and character classification rules.  If LC_COLLATE or LC_CTYPE is defined their definition supercedes the defini-
		      tion of LANG.

Options
       -a[n]	   Write lines that contain unmatched join fields to the output file.  You can cause the command to  write  unmatched  lines  from
		   only  one  file  using  n.  If you specify 1 in n, writes unmatched lines only from file 1.	If you specify 2, writes unmatched
		   lines only from file 2.

		   If you omit the option, writes no output for unmatched lines.

       -e s	   Writes the string you specify in s to the output if you specify a nonexistent field in the list for the option.   For  example,
		   if lines in file 2 contain only three fields, and you specify 2.4 in list, writes s in place of the nonexistent field.

       -jn m	   Defines  field  m  in file n to be the join field. The command compares the field you specify in the option to the default join
		   field in the other file.  If you omit n, the command uses the mth field in both files.

       -1 m	   Use the m th field in the first file as the join field.  This option is equivalent to using m.

       -2 m	   Use the m field in the second file as the join field.  This option is equivalent to using m.

       -o list	   Output the joined data according to list.  The specifiers in list have the format file.field, where file is either 1 or  2  and
		   field is the number of the field.

       -tc	   Recognize the tab character c.  The presence of c in a line is significant, both for comparing join fields and creating output.

Restrictions
       If you specify the option, the command collates the same as with no options.

Examples
       Suppose that by issuing the following commands, you display the files shown in the example:
       % cat file_1
       apr     15
       aug     20
       dec     18
       feb     05
       % cat file_2
       apr     06
       aug     14
       date
       feb     15
       Both files are sorted in ascending order.

       If you issue the command without options, the output appears as follows:
       % join file_1 file_2
       apr 15 06
       aug 20 14
       feb 05 15
       The third line in each input file is not joined in the output because the join fields (date and dec) do not match.

       To  join  the  lines  in these files and format the output so that the second field from each file appears first and the first (join) field
       appears second, issue the following command:
       % join -o 1.2 1.1 2.2 2.1 file_1 file_2
       15 apr 06 apr
       20 aug 14 aug
       05 feb 15 feb
       To write lines that are unmatched to the output, issue the following command:
       % join -a file_1 file_2
       apr 15 06
       aug 20 14
       date
       dec 18
       feb 05 15

See Also
       awk(1), comm(1), sort(1), sort5(1), environ(5int)

																	   join(1)
All times are GMT -4. The time now is 12:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy