Extracting values based on line-column numbers from multiple text files

Dear All,
I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional).
I have multiple tab-delimited files with the following structure:
file1:

1    44
2    56  46
3    90  56  43
4    32  32  23  49
5    43  56  45  34  48
6    35  32  56  41  42  43
7    38  32  23  12  56  32  67
8    78  32  45  45  12  45  45  45
9    23  45  45  34  43  34  12  23  234
10   45  62  56  44  77  54  33  78  890  432
11   12 43   21  34  54  43  56  54  543  546  65   
.
.

I have to do the following 2 task with these files:
Task1:
I want to pick up a specific value (based on line and column number) from each file and put it to another file (say Task1.txt). In Task1.txt I have defined that for each file I want the value in what line and what column and the value should be added to the next column of this file.
Task1.txt

FileName      lineNumber         ColumnNumber     value  
File1               5                  4           45
File2               7                   6            ?
File3               10                  9            ?
File4               67                  55           ? 

Task2
For the same files I want to pick up all values of the second column for a range of lines and put them into the Task2.txt file. Here is the Task2.txt file:
Task2.txt

FileName   Line-start   line-end
File1          4             8         32     43     35     38     78
File2          67           80
File3          345          380
File4          48            54

It will be appreciated if you kindly help me to solve these issues.
Regards

Any ideas/thoughts/attempts from your side?

---------- Post updated at 13:12 ---------- Previous update was at 13:10 ----------

Is there always just one single line for one single file in task1 / task2?

1 Like

Thank you for your reply. Actually I am a sort of newbie to Linux.
Yes, in Task 1 and Task2 files there is always a single line for each file.
Regards

first approximation:

awk '
FNR == 1        {FC++
                 FN = FILENAME
                 if (FC < 3)    {OUT[FC] =  FN ".new"
                                 print > OUT[FC]
                                 next
                                }
                 printf "%s", LIN[FN,1] > OUT[1]
                 printf "%s", LIN[FN,2] > OUT[2]
                }
FC < 3          {LIN[$1,FC] = $0
                 BEG[$1,FC] = $2
                 STP[$1,FC] = $3
                 next
                }

FNR == BEG[FN,1]        {printf "\t%s\n", $STP[FN,1] > OUT[1]   
                        }
FNR == BEG[FN,2], \
FNR == STP[FN,2]        {printf "\t%s", $2 > OUT[2]
                        }
FNR == STP[FN,2]        {printf "\n"  > OUT[2]
                        }
'  task[12] file1
task1.new:
FileName    lineNumber    ColumnNumber    value    
file1    5    4    45
task2.new:
FileName    Line-start    line-end
file1    4    8    32    43    35    38    78
2 Likes

Your are great RudiC!! It nicely works.
Many thanks for your valuable help.

This one wouldn't need the file supplied on the command line but would try to find the files named in task1 or task2:

awk ' 
FNR == 1        {FC++
                 FN = FILENAME
                 if (FC < 3)    {OUT[FC] =  FN ".new"
                                 printf "%s", $0 > OUT[FC]
                                 next
                                }
                 printf "\n%s", LIN[FN,1] > OUT[1]
                 printf "\n%s", LIN[FN,2] > OUT[2]
                }
FC < 3          {LIN[$1,FC]   = $0
                 BEG[$1,FC]   = $2
                 STP[$1,FC]   = $3
                 for (i=0; i<=ARGC && $1 != ARGV; i++);
                 if  ((i > ARGC) && (1 == (getline X < $1))) ARGV[ARGC++] = $1
                 close ($1)
                 next
                }

FNR == BEG[FN,1]        {printf "\t%s", $STP[FN,1] > OUT[1]
                        }

FNR == BEG[FN,2]        {L = 1
                        }
L                       {printf "\t%s", $2 > OUT[2]
                        }
FNR == STP[FN,2]        {L = 0
                        }

END                     {printf "\n" > OUT[1]
                         printf "\n" > OUT[2]
                        }
'  task[12]
1 Like