Filtering values in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filtering values in variable
# 1  
Old 07-26-2013
Filtering values in variable

Hi, is there a faster/simpler way to filter values from the variable1 in variable2?

example:
Code:
variable1="A|B|C|E"
variable2="A|B|C|D|F"

output: "A|B|C"

Thanks,
zzavilz
# 2  
Old 07-26-2013
Using bash:
Code:
#!/bin/bash

variable1="A|B|C|E"
variable2="A|B|C|D|F"

for v1 in ${variable1//\|/ }
do
        for v2 in ${variable2//\|/ }
        do
                if [ "$v1" == "$v2" ]
                then
                        if [ -z "$variable3" ]
                        then
                                variable3="$v1"
                        else
                                variable3="${variable3}|${v1}"
                        fi
                fi
        done
done

echo "$variable3"

# 3  
Old 07-26-2013
Quote:
Originally Posted by zzavilz
Hi, is there a faster/simpler way ...
Faster/simpler relative to what? You have not shown any code.

Regards,
Alister
# 4  
Old 07-26-2013
Are identical chars always in identical positions or can they permute:
Code:
A|B|C
A|C|E

should that yield A|C or not?

Last edited by RudiC; 07-28-2013 at 01:31 PM.. Reason: typo
# 5  
Old 07-29-2013
how about below phthon

Code:
a="A|B|C|D|E|F|G"
b="A|B|C|D|Ea"
s=''
for i in range(max(len(b),len(a))):
 try:
  if a[i]==b[i]: s+=a[i]
 except:
  break
print(s)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering based on column values

Hi there, I am trying to filter a big file with several columns using values on a column with values like (AC=5;AN=10;SF=341,377,517,643,662;VRT=1). I wont to filter the data based on SF= values that are (bigger than 400) ... (25 Replies)
Discussion started by: daashti
25 Replies

2. Shell Programming and Scripting

Variable filtering in awk

Hello all, can you explain why this filter does not work, it prints all the lines in the file: awk -v sel="TestString" 'sel' file while these work: awk '/TestString/' file awk -v sel="TestString" '$0~sel' file Thanks!:) (6 Replies)
Discussion started by: gio001
6 Replies

3. Shell Programming and Scripting

Filtering my major and minor values

I want to remove all rows with a minor repeating count less than 30% compared to the major repeating count from my table. The values of a col(starting col 2) can assume is A,T,G,C and N. Each row has at least 2 values and at most 4 repeating values(out of ATGC). N is considered a missing value... (12 Replies)
Discussion started by: newbie83
12 Replies

4. Shell Programming and Scripting

swapping the values of variable!

Hi All, newbie here, i'm just wondering how can i swap the two values of variables without using the third variable. Please advise, Thanks, (5 Replies)
Discussion started by: nikki1200
5 Replies

5. Shell Programming and Scripting

Perl: filtering lines based on duplicate values in a column

Hi I have a file like this. I need to eliminate lines with first column having the same value 10 times. 13 18 1 + chromosome 1, 122638287 AGAGTATGGTCGCGGTTG 13 18 1 + chromosome 1, 128904080 AGAGTATGGTCGCGGTTG 13 18 1 - chromosome 14, 13627938 CAACCGCGACCATACTCT 13 18 1 + chromosome 1,... (5 Replies)
Discussion started by: polsum
5 Replies

6. Shell Programming and Scripting

Variable with several values and spaces.

Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted. The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names... (8 Replies)
Discussion started by: spoovy
8 Replies

7. Shell Programming and Scripting

Filtering some data and storing the remaing to a variable.

Hi, i have a problem writing a script. Actually i have many files which contains some data, now the last line has some data as in the format : 2556||04-04-10 now i want to do tail -1 filename.txt and store the result into a variable which will be used later for some other calculations. My... (7 Replies)
Discussion started by: fidelis
7 Replies

8. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

9. UNIX for Dummies Questions & Answers

AWK (NAWK) and filtering values

Hi , i try to filter input file : 17/04/2008 06:17:09 17/04/2008 00:00:02 keeping lines with hour > 06 as : 17/04/2008 06:17:09 i tried : CSL=06 nawk -v CSL="${CSL}" -F'' '/^\[/ { if ( $4 -gt $CSL) print $0 } ; /^\>/ { if ( $5 -gt $CSL) print $0 }' input_file.txt... (11 Replies)
Discussion started by: Nicol
11 Replies

10. Shell Programming and Scripting

Substitute variable values

Hi, I am trying to redefine the value of a variable based on another variable value. And I want to read in my variables from a enviroment file in the end -- at least I think so. But 1st here's what I want I need to get working: var_1="11 10 9 8 7 6 5 4 3 2 1" var_2=3 var_3=4 So I want... (12 Replies)
Discussion started by: John Rihn
12 Replies
Login or Register to Ask a Question