Assign variables with cut


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign variables with cut
# 1  
Old 09-23-2004
Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is:

Code:
awk '{print $3}' file1 > file2
awk '{print $4}' file1 > file3

cat file2 | while read var1
do
   cat file3 | cut -c2 | while read var2
      do
         if [[ "$var1" = "L" && "$var2" = "e" ]] then
            echo "Good Match" $var1

         elif [[ "$var1" = "T" && "$var2" = "t" ]] then   
            echo "Good Match" $var1
         else 
            echo "No Match" 

         fi
      done
done

It seems like my results are 2 running twice (reading the var2 twice), meaning the test runs separately for both tests.. I need to link up the variable lists in order to test both conditions at the same time (or dependant on one another). TIA.
# 2  
Old 09-23-2004
Just read the original file and discard some variables -
try:
Code:
   while read first second var1 var2 alltherest
      do
         if [[ "$var1" = "L" && "$var2" = "e" ]] then
            echo "Good Match" $var1

         elif [[ "$var1" = "T" && "$var2" = "t" ]] then   
            echo "Good Match" $var1
         else 
            echo "No Match" 
         fi
      done < file1

# 3  
Old 09-27-2004
thank you very much, I was not aware that we could assign 2 variables in that manner. Here is my code that works, for those who may run into this problem:

Code:
cat file | grep "M " > file1

awk '{print $3,$4}' file1 > file2

cat file2 | cut -c1,2,4 > file3

cat file3 | while read var1 var2  

do
   if [[ "$var1" = "L" && "$var2" = "e" ]] then
      echo "Good Match" $var1
   elif [[ "$var1" = "T" && "$var2" = "t" ]] then   
      echo "Good Match" $var1
   elif [[ "$var1" = "P" && "$var2" = "c" ]] then
      echo "Good Match" $var1 
   else 
      echo "No Match"
   fi
done


Last edited by douknownam; 09-27-2004 at 04:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

How to assign value to variable using cut?

Hey guys. Below is the command I am using to assign "yellow" to the variable yellow. I can seem to echo it out using xargs but when I assign it to yellow and then try to echo it out it prints a blank line. Below is the command. Your help is highly appreciated! cut -c 2- color.txt |... (11 Replies)
Discussion started by: eldan88
11 Replies

3. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

4. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

5. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

6. Shell Programming and Scripting

Cut text and assign them into array

file.txt : is delimiter: abc:def:ghi jkl:mno: pqr 123:456:789 if I do the cut command, and cut the first column, and echo it out I will get the output: abc jkl 123 How can I assign the column of text that I've cut into Array? e.g If I were to echo array array it will output as:... (9 Replies)
Discussion started by: andylbh
9 Replies

7. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

8. UNIX for Dummies Questions & Answers

Cut Command value assign to variable

Hi, I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful. Sample Header: HJAN BALANCE 20090616 I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies

9. Shell Programming and Scripting

Assign script parameters to variables

Hi, I have a bash script that accepts some parameters as input, like: sh script.sh first second third ..... I want to save these parameters as different variables, something like: VAR1=first VAR2=second etc I tried this, but apparently it didn't worked....... (16 Replies)
Discussion started by: giorgos193
16 Replies

10. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies
Login or Register to Ask a Question