I have a file input.txt
which contains multiple filename in the below format.
FILENAME_DATE_LINENUMBER
, the input.txt
contains many such filenames.
The filename itself has precisely 5 underscore.
FILE_NAME_1.DAT_20180123_4
FILE_NAME_2.DAT_20180123_5
FILE_NAME_3.DAT_20180123_6
FILE_NAME_4.DAT_20180123_7
All files are present in sub directory as input.txt
. I want to parse input.txt
, iterate through each filename and print FILENAME and the specified line number ( from the FILENAME ) to output.txt
I understand that sed or awk will be used , and below command can do the job.
awk 'FNR==LINENUMBER {print FILENAME, $0}' *.txt >output.txt
But how can i iterate through the file input.txt
and find the FILENAME and extract LINENUMBER from FILENAME to output.txt
The FILENAME specified in input.txt
can in one of the sub directories where input.txt
is located. There can be only one file with FILENAME in input.txt inside one of the sub directory ( one level ) form the input.txt
location.
DIR
├── input.txt
│ ├── DIR1
│ │ ├── FILE_NAME_1.DAT
│ ├── DIR2
│ │ ├── FILE_NAME_2.DAT
│ ├── DIR3
│ │ ├── FILE_NAME_3.DAT
In output.txt
it should be printed as
FILENAME
LINE( Extracted from FILENAME present in input.txt )
FILE1.DAT_20180123_4
transformed intoFILE1.DAT 4
?FILENAME LINE(under number)
- update your questionFILE*.DAT...
are in the same directory asinput.txt
?