bash matching two files lines by lines
hello people, i have two files,
one with a,b,c d,e,f the other 1 2 I need to do get in one file something like a,1 b,1 c,1 d,2 e,2 f,2 i did a nested for loop but it doesn't work, and i have no idea of who to solve these problem. I tried to read by a line number but no succeeded. thank you very much..... hugs |
Assuming both files have the same number of lines you could use sdiff to do side by side comparision then massage the output to get what you want:
Code:
sdiff file1 file2 |awk '{print $1","$3} |
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 |
awk:
Code:
awk -F, -vnum_file="$temp_num" '{getline num < num_file; Code:
# open files for reading |
Quote:
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. |
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" thank you very much |
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 |
All times are GMT -5. The time now is 04:26 AM. |