Crunch character combination and discard similar content


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Crunch character combination and discard similar content
# 1  
Old 01-29-2015
Crunch character combination and discard similar content

Hi guys !

I generated the power set of the set S={a,b,c} using crunch:
Code:
crunch 1 3 abc

and get the 39 possible subsets:
Code:
a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
…

As the order of the character in the string is not important, I am trying to get rid of the subsets containing the same characters and keep only one (see before: green, the one to keep; red the ones to discard).
In order to obtain:
Code:
a
b
c
aa
ab
ac
bb
bc
cc
...

Any language is fine.
If someone could put me on the track !

---------- Post updated at 08:04 PM ---------- Previous update was at 07:34 PM ----------

!!! Solved !!!

Was just a matter of sorting string horizontally (Sort each row (horizontally) in AWK or any)

---------- Post updated at 09:15 PM ---------- Previous update was at 08:04 PM ----------

Another question related to this though with a slightly different input.
input:
Code:
a|-|1
b|-|2
a|a|3
a|b|4
b|b|5
b|a|4

How could I sort the first 2 fields (FS="|") horizontally and maintain the third one?
ouput:
Code:
a|-|1
b|-|2
a|a|3
a|b|4
b|b|5
a|b|4

I tried this but I don't understand how I can define an array without using the split function:
Code:
gawk 'BEGIN{FS=OFS="|"}{a[$1FS$2FS$3FS$4]=n; asort(n); for(i=1;i<=length(n); i++) printf("%s", n[i]); printf(OFS $5 "\n")}' input

and:
Code:
 gawk 'BEGIN{FS=OFS="|"}{array[a]=$1FS$2FS$3FS$4; asort(array); for(i=1;i<=length(array); i++) printf("%s", array[i]); printf("%s%s\n", OFS, $5)}' input

returns:
Code:
a|-|-|-|1
a|-|-|-b|-|-|-|2
a|-|-|-a|a|-|-b|-|-|-|3
a|-|-|-a|a|-|-a|b|-|-b|-|-|-|4
a|-|-|-a|a|-|-a|b|-|-b|-|-|-b|b|-|-|5
a|-|-|-a|a|-|-a|b|-|-b|-|-|-b|a|-|-b|b|-|-|4


Last edited by beca123456; 01-30-2015 at 12:11 AM..
# 2  
Old 01-30-2015
Code:
awk -F"|" -v OFS="|" ' $1 > $2 && $2 != "-" { t=$2;$2=$1;$1=t } 1 ' file

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 02-01-2015
Thanks anbu23 !

It works here with 2 fields, however I am trying to use gawk asort function instead as I can have more than 2 fields to sort horizontally.
e.g.:
Code:
a|-|-|-|1
b|-|-|-|2
a|b|c|d|3
a|c|d|b|4
b|a|c|d|5

With your method it would be a massive code to write because of all possible combinations!

output:
Code:
a|-|-|-|1
b|-|-|-|2
a|b|c|d|3

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Command combination for displaying header and content

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You are given a ANSI text file (a.txt) in the following format and with the following contents-: NAME ROLL... (7 Replies)
Discussion started by: sreyan32
7 Replies

2. UNIX for Dummies Questions & Answers

Command combination for displaying header and content

Hi everyone, I have UNIX this semester and I am just getting started with the commands. An interesting question came up while discussing the head and tail commands. Suppose that I have text file with the following data in the following format-: NAME ROLL MARKS Sam 05 ... (2 Replies)
Discussion started by: sreyan32
2 Replies

3. Shell Programming and Scripting

Number some lines discard others?

Hi, I'd like to do an operation on text with a format like this this line shall be numbered this line shall not be numbered this line shall also be numbered this line shall not not be numbered And I want an output like this 1 this line shall be numbered this line... (6 Replies)
Discussion started by: jeppe83
6 Replies

4. Shell Programming and Scripting

how to find pattern and discard lines before it?

Hi all, I'd like to search a file for the first occurence of the phrase "PLASTICS THAT EXPIRE" and then discard all the lines that came before it. Output the remainder to a new file. Operating system is hp-ux. I've searched for usual awk and sed one liners but can't find a solution. Thank... (4 Replies)
Discussion started by: Scottie1954
4 Replies

5. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

6. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

7. Shell Programming and Scripting

sed discard chars after last _

Hi, I'd get fields like unix_linux_form_yyyyddmmhhmi.file.txt shell_programming_and_scripting.txt so on... and want them as below unix_linux_form shell_programming_and I could remove everything after a '.' as below echo $field | sed 's/\..*//' but how to remove... (14 Replies)
Discussion started by: dips_ag
14 Replies

8. Shell Programming and Scripting

display content between all similar tags pattern

hi, I m stuck at a point for more than 3days. My doubt is pretty simple.. I have a web page content in $content. ( got this by using LWP).. Now I want to display the content matching a pattern. I tried if($content =~ m{<div class="abc">(.*?)</div>}s){ print $1;} that will... (4 Replies)
Discussion started by: therockravi
4 Replies

9. Linux

Command that prints content of line5, or similar?

Hello all; I've been having trouble completing a script (bash) because I can't get past the issue of finding a line in a file. For example, I have a file like this: ddmmmyyyy Lon Lat 24may2003 -100.0 24.1 25may2003 -100.1 24.0 28may2003 -99.5 23.2 ....etc... (4 Replies)
Discussion started by: lunchtime
4 Replies
Login or Register to Ask a Question