Like I said I'm new to this and I tried to explain it the best I could. I'll try to give the full run down. I have a 46 column data input file that looks like this:
3701 09 39 51.42 85 22 48.02 15.544 0.661 16.349 0.0368 16.193 0.0122 15.535 0.0117
3800 09 35 45.07 85 14 19.24 15.908 0.728 16.808 0.0320 16.615 0.0124 15.906 0.0117
3825 09 44 39.06 85 12 04.66 16.113 0.734 17.046 0.0319 16.823 0.0123 16.107 0.0117
3829 09 39 52.06 85 11 44.99 16.252 0.787 17.295 0.0334 17.015 0.0125 16.246 0.0117
3935 10 16 09.40 85 22 22.69 16.246 0.744 17.229 0.0314 16.968 0.0120 16.237 0.0117
3942 10 02 09.58 85 21 58.22 14.464 1.043 16.348 0.0306 15.484 0.0123 14.453 0.0116
3977 10 08 41.28 85 19 20.60 16.165 0.733 17.149 0.0358 16.874 0.0121 16.153 0.0117
3978 09 54 39.79 85 19 16.53 15.980 0.804 17.183 0.0307 16.754 0.0121 15.963 0.0117
3979 10 19 42.93 85 19 12.80 16.170 0.828 17.424 0.0360 16.977 0.0104 16.159 0.0113
4006 09 52 01.37 85 18 18.69 14.936 1.012 16.688 0.0335 15.919 0.0122 14.928 0.0117
4014 09 57 00.09 85 17 49.63 16.231 0.730 17.208 0.0361 16.932 0.0122 16.223 0.0117
4063 10 14 22.03 85 14 37.34 15.716 0.707 16.614 0.0263 16.400 0.0103 15.711 0.0112
4065 10 11 55.20 85 14 32.15 15.470 0.679 16.314 0.0262 16.127 0.0104 15.464 0.0113
4075 09 52 56.26 85 13 41.39 15.698 0.660 16.498 0.0256 16.339 0.0120 15.690 0.0115
4119 10 20 50.27 85 09 55.13 15.772 0.713 16.645 0.0285 16.463 0.0119 15.759 0.0117
4163 10 51 58.58 85 26 10.99 15.572 0.672 16.408 0.0262 16.228 0.0129 15.575 0.0125
4177 10 53 05.07 85 25 04.96 15.344 0.670 16.165 0.0253 16.009 0.0098 15.344 0.0096
4200 10 49 28.47 85 23 02.22 14.927 0.763 16.024 0.0102 15.651 0.0071 14.903 0.0084
4228 10 35 44.83 85 20 33.71 13.942 1.047 15.784 0.0217 14.963 0.0091 13.927 0.0110
4240 11 00 20.23 85 19 56.52 15.988 0.698 16.890 0.0299 16.659 0.0086 15.975 0.0092
4248 10 27 30.88 85 19 14.33 15.848 0.695 16.719 0.0287 16.518 0.0104 15.836 0.0113
4250 10 44 38.15 85 19 06.81 15.830 0.676 16.658 0.0276 16.472 0.0086 15.813 0.0092
4254 10 37 33.60 85 18 38.93 15.816 0.684 16.667 0.0275 16.480 0.0103 15.805 0.0113
4257 10 58 11.40 85 18 35.19 14.914 0.713 15.861 0.0159 15.599 0.0079 14.897 0.0089
4267 10 58 58.07 85 18 07.43 15.335 0.693 16.209 0.0242 15.999 0.0085 15.323 0.0090
4268 10 52 53.96 85 17 57.84 14.916 0.742 15.922 0.0215 15.621 0.0085 14.900 0.0090
Obviously I didn't include all 46 columns and 200-some rows but you get the idea. What I want to be able to do is write a script that asks for which columns to take from the input and write to the output. The problem is I could want 4 columns or I could want 10 columns to be written out. I have the code first prompt for the input file name so I don't need to hard-code that, and for the user to give whatever output file name you want.
# 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
Notice that I remove the first 3 lines and the last line from the input file just because those are some header lines that I don't need. Next I have the code ask for how many columns to be copied to the output file, and I store that in the variable num:
# Get number of columns to be included
echo "Enter how many columns to be used"
read num
Now that the script knows how many columns I want to take from the input file, I use a for loop to ask which columns are to be copied:
echo "Provide the desired columns to be included Press [enter] after each"
for (( i = 0 ; i < $num; i++ ))
do
read choices
done
No matter which columns I choose I always want to print the first column of the input file to the first column of the output file so I use this:
awk ' { print $1, " " } ' infilemod >temp1
So at this point I have a file called temp1 that has just the first column printed with a space after. Now I use another loop to try to loop over the choices of columbs to be printed:
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
The way I've written it is just so that awk adds columns to the file temp1 rather than writing over top of them. You can think of it as put in first column of the input file, then put in each column from the input file that you specified. This is where the problem comes in though. It does not print the column c as I have it written. I just get the blank spaces. Very frustrating!
Finally I have the code put in a header line to the output file that I have stored in another file called header.txt and remove the temp files that I used.
# Add header
cat header.txt temp1 > tmp; mv tmp $outfile
# Remove extra files
rm infilemod
rm temp1
exit 0
Hopefully this is clearer now. Sorry about all the confusion. I really appreciate the help!