Sponsored Content
Top Forums Shell Programming and Scripting Comparison and editing of files using awk.(And also a possible bug in awk for loop?) Post 302439083 by linuxkid on Wednesday 21st of July 2010 05:17:04 PM
Old 07-21-2010
Question 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:

Code:
pictures.txt 1.1 1.3
dance.txt 1.2 1.4
treehouse.txt 1.3 1.5


File2:
Code:
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 ref4948 1.1
treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838 1.3 ref4738 1.2 ref4573 1.1

What I want is the file and the version number to be printed into a file between the two version numbers in shown in file1. So the output should be:


Code:
pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948
treehouse.txt 1.5 ref3284 1.4 ref5838 1.3 ref4738


Also, rdcwayx identified a possible bug in the awk for loop which no one seems to have picked up on:

Code:
$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3

$ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}'
15
14
13
12

In the first example there should be 4 outputs, however only 3, whilst in the 2nd one we get all 4.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String Comparison between two files using awk

I have two files with field seperator as "~". File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank... (7 Replies)
Discussion started by: rudoraj
7 Replies

2. Shell Programming and Scripting

Comparison of two files in awk

Hi, I have two files file1 and file2 delimited by semicolon, And I want to compare column 2 and column3 of file1 to column3 and column 4 in file2. file1 -------- abc;cef;155.67;143_34; def;fgh;146.55;123.3; frg;hff;134.67;; yyy;fgh;134.78;35_45; file 2 --------- abc;cef;155.09;;... (12 Replies)
Discussion started by: jerome Sukumar
12 Replies

3. Shell Programming and Scripting

Awk Comparison of 2 specific files

Hi Everybody, I know the topic sounds familiar but I just couldn't adapt or find the right code that solves my particular issue. I really hope you can help. I would like to compare 2 files in an awk script. Both files have different paths. The awk script call should look like that awk -f... (7 Replies)
Discussion started by: hhoosscchhii
7 Replies

4. Shell Programming and Scripting

For loop in awk, a bug or something else?

Seems for loop in awk has bug? For first awk, i expect to get 4 output: 1.5, 1.4, 1.3, 1.2, but there are only 3. $ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}' 1.5 1.4 1.3 $ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}' 15 14 13 12 (1 Reply)
Discussion started by: rdcwayx
1 Replies

5. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

6. Shell Programming and Scripting

comparison of 2 files using unix or awk

Hello, I have 2 files and I want them to be compared in a specific fashion file1: A_1200_1250 A_1251_1300 B_1301_1350 B_1351_1400 B_1401_1450 C_1451_1500 and so on... file2: 1210 1305 1260 1295 1400 1500 1450 1495 Now The script should look for "1200" from A_1200_1250 of... (8 Replies)
Discussion started by: Diya123
8 Replies

7. Shell Programming and Scripting

For Loop Field editing - without using "awk"

Hi, I'm using Linux and bash shell. I have a file (F1.txt) with contents like Table1 Column1 123abc Table1 Column2 xyz Table2 Column1 543 Now, I would like to get the output as UPDATE Table1 SET Column1='123abc'; UPDATE Table1 SET Column2='xyz'; UPDATE Table2 SET Column1='543';... (3 Replies)
Discussion started by: Dev_Dev
3 Replies

8. Shell Programming and Scripting

Awk: Replacement using 2 diff files input and comparison

Requirement: If $5(date field) in ipfile is less than $7(date field) in deact file & $1 of ipfile is present in deactfile then $1 to be replaced by $2,$3,$4,$5,$6 of deact file else if $5(date field) in ipfile is greater than $7(date field) in actfile & $1 of ipfile is present in actfile then... (5 Replies)
Discussion started by: siramitsharma
5 Replies

9. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

10. UNIX for Beginners Questions & Answers

awk comparison using multiple files

Hi, I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below. Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in... (2 Replies)
Discussion started by: grv
2 Replies
A2P(1)							 Perl Programmers Reference Guide						    A2P(1)

NAME
a2p - Awk to Perl translator SYNOPSIS
a2p [options] [filename] DESCRIPTION
A2p takes an awk script specified on the command line (or from standard input) and produces a comparable perl script on the standard output. OPTIONS Options include: -D<number> sets debugging flags. -F<character> tells a2p that this awk script is always invoked with this -F switch. -n<fieldlist> specifies the names of the input fields if input does not have to be split into an array. If you were translating an awk script that processes the password file, you might say: a2p -7 -nlogin.password.uid.gid.gcos.shell.home Any delimiter can be used to separate the field names. -<number> causes a2p to assume that input will always have that many fields. -o tells a2p to use old awk behavior. The only current differences are: o Old awk always has a line loop, even if there are no line actions, whereas new awk does not. o In old awk, sprintf is extremely greedy about its arguments. For example, given the statement print sprintf(some_args), extra_args; old awk considers extra_args to be arguments to "sprintf"; new awk considers them arguments to "print". "Considerations" A2p cannot do as good a job translating as a human would, but it usually does pretty well. There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument is always going to be integer, so it leaves it in. You may wish to remove it. Perl differentiates numeric comparison from string comparison. Awk has one operator for both that decides at run time which comparison to do. A2p does not try to do a complete job of awk emulation at this point. Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment ""#???"". You should go through and check them. You might want to run at least once with the -w switch to perl, which will warn you if you use == where you should have used eq. Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. If somehow you are relying on this mechanism to create null entries for a subsequent for...in, they won't be there in perl. If a2p makes a split line that assigns to a list of variables that looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n option mentioned above. This will let you name the fields throughout the script. If it splits to an array instead, the script is probably referring to the number of fields somewhere. The exit statement in awk doesn't necessarily exit; it goes to the END block if there is one. Awk scripts that do contortions within the END block to bypass the block under such circumstances can be simplified by removing the conditional in the END block and just exiting directly from the perl script. Perl has two kinds of array, numerically-indexed and associative. Perl associative arrays are called "hashes". Awk arrays are usually translated to hashes, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. Iteration over a hash is done using the keys() function, but iteration over an array is NOT. You might need to modify any loop that iterates over such an array. Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. You'll want to set $# explicitly if you use the default value of OFMT. Near the top of the line loop will be the split operation that is implicit in the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. For aesthetic reasons you may wish to change index variables from being 1-based (awk style) to 0-based (Perl style). Be sure to change all operations the variable is involved in to match. Cute comments that say "# Here is a workaround because awk is dumb" are passed through unmodified. Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. Scripts that refer to the special variables RSTART and RLENGTH can often be simplified by referring to the variables $`, $& and $', as long as they are within the scope of the pattern match that sets them. The produced perl script may have subroutines defined to deal with awk's semantics regarding getline and print. Since a2p usually picks correctness over efficiency. it is almost always possible to rewrite such code to be more efficient by discarding the semantic sugar. For efficiency, you may wish to remove the keyword from any return statement that is the last statement executed in a subroutine. A2p catches the most common case, but doesn't analyze embedded blocks for subtler cases. ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n-1]. A loop that tries to iterate over ARGV[0] won't find it. ENVIRONMENT
A2p uses no environment variables. AUTHOR
Larry Wall <larry@wall.org> FILES
SEE ALSO
perl The perl compiler/interpreter s2p sed to perl translator DIAGNOSTICS
BUGS
It would be possible to emulate awk's behavior in selecting string versus numeric operations at run time by inspection of the operands, but it would be gross and inefficient. Besides, a2p almost always guesses right. Storage for the awk syntax tree is currently static, and can run out. perl v5.18.2 2014-01-06 A2P(1)
All times are GMT -4. The time now is 06:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy