ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I im writing a bash script to read a file and make a statistics from the contents.
The file have severeal lines and each line 100 fields divide by comma and I need to read only the field 32 and 34 from all lines. The problem is length of the fields are varibles
Thanks for looking over the code, and pointing out the error; it always helps to have more than one pair of eyes looking at things.
I changed the data file to highlight the error, and changed the script to call attention to the error:
Code:
#!/bin/bash
# @(#) s2 Demonstrate parsing with -- and setting IFS.
F=${1-data1}
echo
echo " Data line:"
head -1 $F
echo
oldifs="$IFS"
IFS=",$IFS"
while read chars
do
set -- $chars
fielda=${32}
fieldb=$34
fieldc=${3}4
fieldd=${34}
echo $fielda
echo $fieldb
echo $fieldc
echo $fieldd
done < $F
IFS="$oldifs"
exit 0
Which produces:
Code:
% ./s2
Data line:
1x,2x,3x,4x,5x,6x,7x,8x,9x,10x,11x,12x,13x,14x,15x,16x,17x,18x,19x,20x,21x,22x,23x,24x,25x,26x,27x,28x,29x,30x,31x,32x,33x,34x,35x,36x,37x,38x,39x,40x,41x,42x,43x,44x,45x,46x,47x,48x,49x,50x
32x
3x4
3x4
34x
Note that Matir's note is correct, using the positional parameter without the braces selects the wrong variable, in this case parameter 3 instead of parameter 34 ... cheers, makyo
Upon reflection, you didn't say much about how you intend to process the data. If it's a small amount and you intend to do the operations in the shell, then Matir's or my method is probably sufficient.
However, if you have a lot of data, if you need to use something other than the shell, or if you will be scaling up the dataset size, then something like ghostdog74's awk suggestion will use fewer resources. You could also use cut directly on the input file in a like fashion ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.