Comparing Lines In Awk (Can I do this:) 
Author Message
 Comparing Lines In Awk (Can I do this:)

Can I do this:

Look through a text file comparing lines.  If there are two lines w/ in
which the first 10 characters are the same, delete one of the lines.

Thanks,
Zach



Tue, 27 Jan 2004 03:44:14 GMT  
 Comparing Lines In Awk (Can I do this:)

Quote:

> Can I do this:

> Look through a text file comparing lines.  If there are two lines w/ in
> which the first 10 characters are the same, delete one of the lines.

Borrowing from Jim M's most recent lesson in another thread maybe this
will do what you want.

 awk  '{chunk = substr($0,1,10)}
data[chunk]++ == 0 {print}' file

cat file

   1 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   2 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   3 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   4 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   5 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   6 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   7 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   8 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   9 this line starts with 1 unique  char
   these lines do not start with 1 unique char

Results:

        awk  '{chunk = substr($0,1,10)}
        data[chunk]++ == 0 {print}' file

   1 this line starts with 1 unique  char
   these lines do not start with 1 unique char
   2 this line starts with 1 unique  char
   3 this line starts with 1 unique  char
   4 this line starts with 1 unique  char
   5 this line starts with 1 unique  char
   6 this line starts with 1 unique  char
   7 this line starts with 1 unique  char
   8 this line starts with 1 unique  char
   9 this line starts with 1 unique  char



Tue, 27 Jan 2004 06:40:13 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. common mistakes in awk: comparing awk with C

2. How to compare two files line-by-line

3. Anyone done anything about comparing methods ...

4. awk -- pattern match a line and the line that follows

5. Can this be done in AWK?

6. How is this done in awk/nawk/gawk?

7. Whats nawk ( compare to awk ? )

8. Can this be done with AWK or ED ?

9. question on comparing date/time in AWK

10. Multi dimensional arrays (awk compared to others)

11. Q: Compare lines of files

12. Compare lines

 

 
Powered by phpBB® Forum Software