Basic Combination Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic Combination Shell Script
# 1  
Old 09-26-2016
Lightbulb Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:
Code:
Black
Blue
Brown
Orange
Red
Yellow
Green
White
Purple
Silver
Grey
Tan

Then print to another file just all of the two-word possible combinations. Example:

Code:
Black,Blue

Anyone want to take a crack at this?


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 09-27-2016 at 05:18 AM.. Reason: Added CODE tags
# 2  
Old 09-26-2016
Using bash:

Code:
#!/bin/bash
while read word
do
  S[${#S[@]}]="$word"
done < infile

for ((x=0;x<${#S[@]};x++))
do
   for ((y=x+1;y<${#S[@]};y++))
   do
         echo "${S[x]},${S[y]}"
   done
done > outfile

Using awk:

Code:
awk '{S[NR]=$0}
END {
   for(x=1;S[x];x++)
      for(y=x+1;S[y];y++)
         print S[x]","S[y]
}' infile

# 3  
Old 09-27-2016
Hi.

Also a script with a utility written in perl:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate combinations of 2 from list of strings in file.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C combination my-columns head tail

FILE=${1-data1}

pl " Input data file $FILE, columnized:"
my-columns $FILE

pl " Results:"
combination 2 $( cat $FILE ) |
tee f1 |
head -3
pe "..."
tail -3 f1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
combination (local) 1.1
my-columns (local) 1.3
head (GNU coreutils) 8.23
tail (GNU coreutils) 8.23

-----
 Input data file data1, columnized:
Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan

-----
 Results:
Black Blue
Black Brown
Black Orange
...
Silver Grey
Silver Tan
Grey Tan

Eliminating the niceties from the perl code leaves the heart of the utility like this:
Code:
perl -w -e '
BEGIN { @a = @ARGV }
use Math::Combinatorics;
print join( "\n", map { join " ", @$_ } combine( 2, @a ) ),
"\n";' $( cat $FILE )

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 09-27-2016
Why not using SQL

I don't know if it's really out of purpose , but you 'll get such result using a single table in SQL.
This is a complete demo using sqlite ( sqllite3 under CYGWIN on my Windows PC

This code is limited to 4 items , just to avoid useless lines of results .
Code:
$ sqlite3
SQLite version 3.14.1 2016-08-11 18:53:32
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table colors (name text ) ;
sqlite> insert into colors values ("Black");
sqlite> insert into colors values ("Blue");
sqlite> insert into colors values ("Brown");
sqlite> insert into colors values ("Orange");
sqlite> select * from colors ;
Black
Blue
Brown
Orange

sqlite> select a.name,b.name from colors a,colors b ;
Black|Black
Black|Blue
Black|Brown
Black|Orange
Blue|Black
Blue|Blue
Blue|Brown
Blue|Orange
Brown|Black
Brown|Blue
Brown|Brown
Brown|Orange
Orange|Black
Orange|Blue
Orange|Brown
Orange|Orange

sqlite>

This is so called a Cartesian product.

Last edited by blastit.fr; 09-28-2016 at 04:57 PM..
This User Gave Thanks to blastit.fr For This Post:
# 5  
Old 09-28-2016
Code:
awk '{
	Arr[i++]=$0
	}
	END{
	for(j=0;j<=i;j++)
	{
		for(k=j+1;k<i;k++)
		{
			print Arr[j],Arr[k]
		}
	}
}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic Shell script - Not working

Hello, This is basic (i think). I am trying to run a shell script which would go into each folder (folder names defined in the list) and after entering would run some commands, once done, come out of the folder and continue until the list ends. Pretty basic and there are bunch of example online. ... (9 Replies)
Discussion started by: Zam_1234
9 Replies

2. Shell Programming and Scripting

Need a little help with my first shell script. Basic image resize script...

Hey everyone, just now joined because I didn't want to go onto Ubuntu forums and start asking about how to write shell scripts. Seems like this is a pretty active forum for exactly what I need. I'm trying to modify a shell script I found online, the end goal is to have it find all files in the... (9 Replies)
Discussion started by: mozzles
9 Replies

3. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

4. Shell Programming and Scripting

Select combination unique using shell script

Hi All, bash-3.00$ gzgrep -i '\ ExecuteThread:' /******/******/******/******/stdout.log.txt.gz <Jan 7, 2012 5:54:55 PM UTC> <Error> <WebLogicServer> <BEA-000337> < ExecuteThread: '414' for queue: 'weblogic.kernel.Default (self-tuning)' has been busy for "696" seconds working on the request... (4 Replies)
Discussion started by: osmanux
4 Replies

5. Shell Programming and Scripting

Basic shell script help

Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines. Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works. Basically Im looking for... (7 Replies)
Discussion started by: kylecn
7 Replies

6. Shell Programming and Scripting

Basic Shell Script Help

Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it. I'm using the BASH shell by the way. Help is appreciated.... (1 Reply)
Discussion started by: c4391
1 Replies

7. Shell Programming and Scripting

Combination of case and If else in shell script

Would it be right forme to combine case statement and if else in one shell script? Would it work? (2 Replies)
Discussion started by: Pauline mugisha
2 Replies

8. Shell Programming and Scripting

shell script basic doubt

hi, I am new script learner, so my basic doubt is , how to store value of any command in a variable example $ ls | wc -l i want to stote the output of this in a variable c. so that i can use c in if else loop. and when do we use " ` " symbol in script.. can anyone also tell for... (5 Replies)
Discussion started by: hi2_t
5 Replies

9. Shell Programming and Scripting

Basic Shell script syntax help

Hi All, I am new to shell scripting. I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting. Thanks Vignesh (2 Replies)
Discussion started by: vignesh53
2 Replies

10. Shell Programming and Scripting

need a quick basic shell script help

im trying to run the below if command ifconfig -a |grep 10.100.120.21 gives me below output inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0 i just want a basic shell which says if above exists then continue how would i do this? (6 Replies)
Discussion started by: eb222
6 Replies
Login or Register to Ask a Question