Print arguments using array elements not working?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print arguments using array elements not working?
# 1  
Old 05-03-2013
Print arguments using array elements not working?

Hey guys,

I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array and I want to use the array in an awk print line where the array gives the columns to print, but it keeps telling me I have incorrect syntax and it's not working. Here is my user input loop which works fine:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 read choices[i]
done

where num is already properly defined. Then I use this:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 awk -v f2=temp1 ' { c = ${choices[$i]}; getline < f2; print $0, c, " "; } ' infilemod >temp2
 mv temp2 temp1
done

If I use something like c=$10 then it prints out the 10th column however many times without any issues, but it doesn't like when I try to loop over the columns of choice using this method. Also just assume that all of my files are properly defined, because like I said it works fine with c=$1 or whatever. Any suggestions?

P.S. there is a fair amount more to this code but everything else is working fine, it's just this issue with defining c as above. Thanks!
# 2  
Old 05-03-2013
Quote:
Originally Posted by shaner
Hey guys,

I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array and I want to use the array in an awk print line where the array gives the columns to print, but it keeps telling me I have incorrect syntax and it's not working. Here is my user input loop which works fine:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 read choices[i]
done

where num is already properly defined. Then I use this:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 awk -v f2=temp1 ' { c = ${choices[$i]}; getline < f2; print $0, c, " "; } ' infilemod >temp2
 mv temp2 temp1
done

If I use something like c=$10 then it prints out the 10th column however many times without any issues, but it doesn't like when I try to loop over the columns of choice using this method. Also just assume that all of my files are properly defined, because like I said it works fine with c=$1 or whatever. Any suggestions?

P.S. there is a fair amount more to this code but everything else is working fine, it's just this issue with defining c as above. Thanks!
Variable expansions do not occur inside single quotes. Try:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 awk -v f2=temp1 -v c="${choices[$i]}" ' { getline < f2; print $0, c, " "; } ' infilemod >temp2
 mv temp2 temp1
done

# 3  
Old 05-06-2013
Quote:
Originally Posted by Don Cragun
Variable expansions do not occur inside single quotes. Try:
Code:
for (( i = 0 ;  i < $num;  i++ ))
do
 awk -v f2=temp1 -v c="${choices[$i]}" ' { getline < f2; print $0, c, " "; } ' infilemod >temp2
 mv temp2 temp1
done

I tried this and it just prints whatever value c is through the loop, so the columns end up as 1 2 3 etc. So I just need to make it recognize that I wanted the cth column of the data file that I'm working with...
# 4  
Old 05-06-2013
Quote:
Originally Posted by shaner
I tried this and it just prints whatever value c is through the loop, so the columns end up as 1 2 3 etc. So I just need to make it recognize that I wanted the cth column of the data file that I'm working with...
You don't need awk's help to do that.

Code:
while read COL1 COL2 COL3
do
...
done < inputfile

# 5  
Old 05-06-2013
Quote:
Originally Posted by Corona688
You don't need awk's help to do that.

Code:
while read COL1 COL2 COL3
do
...
done < inputfile

Could you elaborate on this? As I mentioned I'm new to shell scripting so I'm not exactly sure what this accomplishes. I did something like
Code:
while read COL1 COL2 COL3
do
echo "cool"
done < NGC188.master.forBayesian.table.full.UBVRI.SM

and it just prints cool however many times, and I get no user input. Also I need to have a variable number of columns so I'm unsure how to accomplish this.
# 6  
Old 05-06-2013
Quote:
Originally Posted by shaner
Could you elaborate on this? As I mentioned I'm new to shell scripting so I'm not exactly sure what this accomplishes. I did something like
Code:
while read COL1 COL2 COL3
do
echo "cool"
done < NGC188.master.forBayesian.table.full.UBVRI.SM

and it just prints cool however many times, and I get no user input. Also I need to have a variable number of columns so I'm unsure how to accomplish this.
We obviously don't understand what you're trying to do.

Please give us a real specification that shows us what the elements of choice[] are set to, what your input file(s) look like, and what the output is that you want to be produced (using code tags for all of these) with an English description of how you expect to convert your input file(s) to your desired output.

Showing us a two small fragments of a shell script that is not working lets us make lots of wild (and obviously incorrect) guesses at what you're trying to do.
# 7  
Old 05-06-2013
Ok my apologies for not being clear enough. I was trying to get right at at but here is the whole code.
Code:
# Get input file from user
echo "Provide name of master file for use"
read infile
sed '1,3d;$d' $infile > infilemod

# Get output file name from user
echo "Provide desired output file name"
read outfile

# Get number of columns to be included
echo "Enter how many filters to be used"
read num

# Get which columns to use
echo "Provide the desired columns to use. Press [enter] after each"
for (( i = 0 ;  i < $num;  i++ ))
do
 read choices[i]
done

# Make file
# Print first column
awk ' { print $1, " " } ' infilemod >temp1

# Selected columns
for (( i = 0 ;  i < $num;  i++ ))
do
 awk -v f2=temp1 -v c=${choices[$i]} ' { getline < f2; print $0, c, " "; } ' infilemod >temp2
 mv temp2 temp1
done

# Add header
cat base9header.txt temp1 > tmp; mv tmp $outfile

# Remove extra files
rm infilemod
rm temp1

exit 0

So what it does is gets the input file from the user, gets an output file name, gets the number of columns you want to take from the input, then gets WHICH of those columns to take, then prints them after printing the first column. I'm sure there is a much more streamlined way to do this but so far this is working except for the printing of the choice columns which I can't seem to make work.

The input file is something like 46 columns of data with 3 lines of header that I cut out. I want the code to be able to streamline the choosing of whatever columns I need to use for my work and put those choice columns into a new file with a header that I add from base9header.txt

Thanks for the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select / Print odd-even elements of an array

Hello experts, I wish to print the contents of odd-even numbered indices of an array. The problem statement is as follows : 1. The first line contains an integer, (the number of test cases). 2. Each line of the subsequent lines containing a string. Example: 2 Haider Bash ... (4 Replies)
Discussion started by: H squared
4 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. UNIX for Dummies Questions & Answers

How to declare an array in UNIX and print the elements with tab delimits?

Hello, In a shell script, I want to declare an array and subsequently print the elements with tab delimits. My array has the following structure and arbitrary elements: myArray=('fgh' 'ijk' 'xyz' 'abc'); Next, I would like to print it with a '\n' at the end. Thanks for your input! ... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

5. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

6. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

7. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

8. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies
Login or Register to Ask a Question