LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2015, 08:03 AM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660

Quote:
Originally Posted by magische_vogel View Post
Is it possible to improve this script to read from pipe instead of files.
I suppose you could use a temporary file (a work file).

With this InFile ...
Code:
41.200.103.2
41.200.103.2
41.200.103.220
41.201.103.2
41.201.103.2
41.201.103.2
41.201.103.220
41.201.103.2
... this code ...
Code:
cat $InFile                     \
|tee $Work1                     \
|awk '{if (2==++a[$0]) print}'  \
|grep -vxf - $Work1             \
>$OutFile
... produced this OutFile ...
Code:
41.200.103.220
41.201.103.220
Daniel B. Martin
 
1 members found this post helpful.
Old 10-22-2015, 09:40 AM   #17
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by pan64 View Post
I do not really understand why do you use 3 different commands if one would be enough:
Code:
sort -u
obviously it is not an answer to the original post, just a comment.
It's really only two commands (since it uses cat)

That being said, sort -u did not work for me with this output (bolded being parts that are supposed to be omitted)

Code:
105.101.173.112
105.97.167.43
105.99.68.250
41.110.184.109
41.200.103.2
 
Old 10-22-2015, 10:10 AM   #18
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by magische_vogel View Post
Your gawk script is not working well with this input (The original order is not kept):

41.200.103.2
105.97.167.43
105.97.167.43
41.110.184.109
41.110.184.109
105.99.68.250
105.99.68.250
41.110.184.109
41.200.103.220
41.110.184.109
41.110.184.109
41.110.184.109
41.110.184.109
105.99.68.250
41.110.184.109
105.99.68.250
105.101.173.112
Ahh yes, it's the old string sort problem. The array bb is indexed by the original line number, but array indices are strings and "17" sorts as less than "9".

If you happen to be running version 4 of gawk there is a trivial fix to add a third argument "@ind_num_asc" to the asorti() call, but for earlier versions you have to make sure the index is a string that will sort properly.
Code:
{ if($0 in aa) aa[$0] = 0; else aa[$0] = FNR }
END {
    for(x in aa) if(aa[x] > 0) bb[sprintf("%15s", aa[x])] = x
    n_unique = asorti(bb, cc)
    for(n = 1; n <= n_unique; ++n) print bb[cc[n]]
}
Padding that index to a 15-character string makes it work (at least until you have 10^15 lines in the input ).
 
1 members found this post helpful.
Old 10-22-2015, 10:19 AM   #19
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
ruby -ane 'h||={}; h[$F[0]] = h[$F[0]].to_i + 1;END{h.each{|k,v| puts k if v == 1}}'
 
Old 10-24-2015, 10:43 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,841

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk '{ ++aa[$0]; x[i++]=$0 } END { for(j=0; j<i; j++) if(aa[x[j]] == 1) print x[j]}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] pattern for grep/sed/awk that matches lines that have several words but not another maddes.b Linux - Software 7 07-03-2013 11:08 AM
[SOLVED] awk/grep/sed/print......etc dotran Linux - Newbie 2 08-08-2012 04:41 PM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 02:58 PM
[SOLVED] awk print only lines {not}containing FS grail Programming 8 04-12-2010 04:41 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration