Pairing up numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pairing up numbers
# 1  
Old 08-16-2012
Pairing up numbers

Hi,

Im trying to script the following logic but having some difficulties and wonder if you can help. Have tried to use "cut" to cut pairs but doesn't appear to do what i need when there are more than one pair. Also "wc -c" doesn't appear to be so easy to know which characters to strip of at

I have a confile file as follows

Code:
#Client|NAME|Username|No_of_Expected_Connections|Instances|Partitioned
JAY|TEST|[JAY]|2|01 02|N|
JAY|TEST2|[JAY]|4|01 02 03 04|Y|
JAY|TEST2|[JAY]|4|01 02 03 04 05 06 07 08|Y|


The logic i want to apply is:

"Check the value for 'Partitioned' in config file. If its Y then get the value of 'Instances' column and pair them up. So if we have following instances "01 02 03 04 05 06" i want to pair "01 02" and "03 04" and "05 06" and do same for the remaining instances

Idea then is that we have log files which would be named based on the above instances. so for pairs "01 02", there would be two log files

logfile01.txt and logfile02.txt
logfile03.txt and logfile04.txt etc"

Please can you help/advice how best to pair these up?

Many Thanks
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 08-16-2012 at 11:19 AM.. Reason: code tags, please!
# 2  
Old 08-16-2012
Code:
#!/bin/bash

while IFS="|" read CLIENT NAME USERNAME CONN INST PART
do
        [ "${CLIENT:0:1}" = "#" ] && continue
        [ "$PART" = "Y" ] || continue

        set -- $INST # If INST="01 02 03 04", $1=01, $2=02, $3=03, $4=04
        MIN=$(($# / 2))
        while [ "$#" -gt 0 ]
        do
                echo "logfile$1.txt" "logfile$2.txt" # Pair the first two
                shift 2 # Get rid of the first two, so $1=$3, $2=$4, ...
        done
done < inputfile

Code:
$ ./cfgfile.sh

logfile01.txt logfile02.txt
logfile03.txt logfile04.txt
logfile01.txt logfile02.txt
logfile03.txt logfile04.txt
logfile05.txt logfile06.txt
logfile07.txt logfile08.txt

$

# 3  
Old 08-16-2012
Thanks Corona688,

Just so I understand this, what does the following do pls?

Code:
        [ "${CLIENT:0:1}" = "#" ] && continue
        [ "$PART" = "Y" ] || continue


Last edited by Scott; 08-16-2012 at 12:56 PM.. Reason: Code tags
# 4  
Old 08-16-2012
&& is a short-form if-then. If the first statement is true, the second is executed. I use it to skip comment lines beginning with #.

|| is a short-form if-not. If the first statement is false, the second is executed. I use it to skip instances which aren't partitioned.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Pairing the nth elements on multiple lines iteratively

Hello, I'm trying to create a word translation section of a book. Each entry in the word list will come from a set of linguistically analyzed texts. Each sentence in the text has the following format. The first element in each line is the "name" of the line (i.e. "A","B","C","D"). The... (12 Replies)
Discussion started by: John Lyon
12 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. Hardware

Bluetooth Dongle Pairing but Not Connecting (Linux)

I have been trying to get bluetooth working correctly on Linux for a while now. I am using two systems that are having identical issues: Linux Mint 15 64-bit & Ubuntu 13.04 32-bit. Both are using the Cinnamon desktop. I have the following bluetooth dongle: ... (6 Replies)
Discussion started by: Deluge
6 Replies

5. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

6. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

7. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

8. Shell Programming and Scripting

Program for pairing together to print outputusing perl

Suppose u have One file one row and one column A1 A2 A3 A4 A1 A2 A3 A4 And another Second file shows pairing A1 A4 A2 A3 A2 A4 A1 A3 Want the output to be like based on the pairing .. As seen from the second fileThose who are paired will get one A1 A4 will... (2 Replies)
Discussion started by: cdfd123
2 Replies

9. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

10. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies
Login or Register to Ask a Question