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.
the problem with that is that the rows in the fist file are of different length, so the $3 in not in all the row the same column.
I did something like these, but is wrong because it reads all the letters for each number, and what i want is for each row, read the other row.
Code:
cat $temp_num | while read num; do
cat $temp_letters | while read lett; do
IFS=,\
array=( $lett )
echo ${#array[@]}
for ((i=0; i<${#array[@]}; i++))
do
echo "( '${array[i]}', '$num')," >> $query
done
done
done
hugs
Last edited by rperezalejo; 01-10-2012 at 10:43 AM.
awk -F, -vnum_file="$temp_num" '{getline num < num_file;
for (i=1; i <= NF; i++) printf("%s,%d\n", $i, num) }' "$temp_letters"
just bash:
Code:
# open files for reading
exec 3< "$temp_num" 4< "$temp_letters"
while read -u3 num && IFS=, read -u4 -a letters ; do
printf "%s,$num\n" "${letters[@]}"
done
# close files
exec 3<&- 4<&-
the problem with that is that the rows in the fist file are of different length, so the $3 in not in all the row the same column.
What I wrote works for the data you provided initially. Note that the $3 is NOT the position in either file but rather the position in sdiff output.
sdiff will output data like:
file1line1 | file1line1
file1line2 | file2line2
In the above file1line# = $1, the pipe sign (|) = $2 and file2line# = $3.
So for your data: a,b,c and d,e,f are $1 and 1 and 2 are $3 for their respective lines.
Therefore what I suggested would work IF the data was formatted as you originally indicated (i.e. comma delimited with no spaces). If you are saying you have variable length lines with embedded whitespace in file1 or file2 then you're correct it wouldn't work.
Last edited by MensaWater; 01-10-2012 at 03:30 PM.
Yes my friend, with the example i wrote it works, but i forgot to put the lines with different length, sorry about that, i found the solution with your help and with the other partner who wrote.
Code:
exec 3< "$temp_pkg" 4< "$temp_class"
while read -u3 pkg && IFS=, read -u4 -a class ; do
for ((i=0; i<${#class[@]}; i++))
do
temp=${class[i]#\ }
echo "('$temp', '$pkg')," >> $query
done
done
exec 3<&- 4<&-
more or less the same, i used both the "for" and "printf" solutions for educational reasons, some time printf is complex to understand.
That's a neat way to use file descriptors. Thanks for posting it!
By the way, bash has the parameter substitution pattern "${!array[@]}", which outputs a list of all existing indexes for the array. You can use it instead of the c-style loop.
Code:
for i in "${!class[@]}"; do
Last edited by David the H.; 01-12-2012 at 06:45 AM.
Reason: added tip
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.