match and sort using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting match and sort using awk
# 1  
Old 03-24-2010
match and sort using awk

Hello,

Is it possible to do match and sort the data from two fields in a file using awk?


Like for example:
input
applecat
inrat
outsky
catpen
ratball
skycpu
penlinux
ballunix
cpupaper
linuxphone
unix 
paper 
phone 
phone 
paper 
unix 
cpu 


desired out put
 
apple
ball ball
cat cat
cpu cpu
cpu
in
linux linux
out
paper paper
paper
pen pen
phone phone
phone
rat rat
sky sky
unix unix
unix


Thanks in advance
# 2  
Old 03-24-2010
need more explain on your question.

for example, why you need export two lines for CPU ? what's the different?

Code:
cpu	cpu
cpu

# 3  
Old 03-24-2010
Hi,

Occasionally some words repeat and also there wont be a match for certain cells in col2

I know that we can extract the contents matching two different files
using

awk 'FNR==NR {a[$1]++;next} $1 in a' file1 file2
but i want to do that in the same file and with spaces if there is no match

diff command dose similar thing but what i had there are just the keywords of a bigger string

Thanks
# 4  
Old 03-24-2010
Does it have to be awk? This is trivial in Perl.
# 5  
Old 03-24-2010
i just started to lean awk and it would be nice if I have a solution to this in awk
but perl is fine too
# 6  
Old 03-24-2010
Your explanation is a little inconsistent. For example, there are three 'cpu' entries, two on left and one on right. What are you expecting the output to be? If the first 'cpu' matches on the right, does that mean the second 'cpu' is no match?

What if there is a word on the right that is not on the left?

If it does not matter, this does what you want:

Code:
$ cat dat.txt
apple cat
in	rat
out	sky
cat	pen
rat	ball
sky	cpu
pen	linux
ball	unix
cpu	paper
linux	phone
unix	 
paper	 
phone	 
phone	 
paper	 
unix	 
cpu
$ 
$ awk '{ f[$1]++; f[$2]++; } END { for (i in f) {for (k=0; k<f[i]; k++) printf i" "; print " " }  }' dat.txt

sky sky  
paper paper paper  
apple  
cat cat  
out  
in  
pen pen  
rat rat  
cpu cpu cpu  
linux linux  
unix unix unix  
ball ball  
phone phone phone

if you just want a diff like comparison, this would do it:

Code:
$ awk '{ f[$1]++; f[$2]++; f1[$1]++; f2[$2]++; } END { for (i in f) { printf i" "; if (f2[i]) { for (k=0; k<f2[i]; k++) printf i" "; } else printf " ";  print " " }  }' dat.txt
         
sky sky  
paper paper  
apple   
cat cat  
out   
in   
pen pen  
rat rat  
cpu cpu  
linux linux  
unix unix  
ball ball  
phone phone

If you want the output to be:

Code:
cpu         cpu
cpu

You will need to match the associative arrays element by element and count by count. I will leave than as an exercise to the awk learner...
# 7  
Old 03-24-2010
Code:
awk 'FNR==NR {a[$2]++;next} {if (a[$1]>0) {print $1,$1;a[$1]--} else {print $1}} ' input input |sort


apple
ball ball
cat cat
cpu
cpu cpu
in
linux linux
out
paper
paper paper
pen pen
phone
phone phone
rat rat
sky sky
unix
unix unix

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk if match

Hi, This is the file content: #160814 20:43:00 server id 2 end_log_pos 169934694 Query thread_id=8927407 exec_time=0 error_code=0 use sun_final/*!*/; SET TIMESTAMP=1471207380/*!*/; DELETE FROM `top_pack` WHERE `top_pack`.`id` = 3023 Trying like:awk... (5 Replies)
Discussion started by: ashokvpp
5 Replies

5. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

6. Shell Programming and Scripting

awk sort

input file abc1 abc23 abc12 abc15 output abc1 abc12 abc15 abc23 (9 Replies)
Discussion started by: yanglei_fage
9 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. Shell Programming and Scripting

Sort in AWK

Hi, I usually use Access to sort data however for some reason its not working. Our systems guys and myself cannot figure it out so ive tried to use AWK to do the sorting. The file is made up of single lines in the format ... (4 Replies)
Discussion started by: eknryan
4 Replies

9. Homework & Coursework Questions

awk sort help

1. The problem statement, all variables and given/known data: I dont know what I do wrong, I am trying to create shell programming database: I have this command first: && > $fname ... echo $Name:$Surname:$Agency:$Tel:$Ref: >> $fname then I have echo " Name Surname Agency Tel... (2 Replies)
Discussion started by: jeht
2 Replies

10. Shell Programming and Scripting

sort & match multiple files

Hi, I have some question and need some guidance how to sort and match multiple files. 1. all the data in the files are numbers e.g. 1234567 1584752 2563156 2. each sorted file have their own ouput. e.g. test.csv -> test_sorted.csv 3. Then, I need to match all... (4 Replies)
Discussion started by: nazri76
4 Replies
Login or Register to Ask a Question