0

I am having basic knowledge in Linux and need your help to develop concept for following requirement. I have two files FILE_NAMES.txt and FILE_NAMES_TS.txt. The content of files as below.

FILE_NAMES.txt
====
jan_
feb_
mar_

FILE_NAMES_TS.txt
====
jan_20170921.csv
feb_20170921.csv
mar_20170921.csv

All the above content are nothing but file names and are placed in db_views/data directory. For the content of FILE_NAMES_TS.txt are 0 bytes initially. Now have to move / rename from jan_ to jan_20170921.csv, and so on. That means the file jan_20170921.txt is now non-zero bytes. Please help.

Srini V
  • 11,045
  • 14
  • 66
  • 89
  • 2
    The question is unclear. You begin with exactly 2 files names `FILE_NAMES.txt` and `FILE_NAMES_TS.txt` and the latter is empty? Or do you begin with many files, including files names `jan_`, `feb_`, etc. and you want to move `jan_` to `jan_20170921.csv` and add an entry into `FILE_NAMES_TS.txt`? – William Pursell Sep 21 '17 at 13:43
  • No need to add entry. Just have to move jan_ to jan_20170921.csv and so on. – K Venkat Sep 21 '17 at 13:46

2 Answers2

0

You can use readarray bash function to read files into 2 arrays and then use mv command to rename each file

#!/bin/bash

readarray a < names.txt
readarray b < names_ts.txt

len=${#a[@]}

echo $len

for (( i=0; i<${len}; i++ ));
do
    echo mv ${a[$i]} ${b[$i]}
done
Richard
  • 1,045
  • 7
  • 11
  • Thank you Richard for sharing code. The command "readarray" will not allow in my current Linux environment. Is there by alternate command to execute the script. Appreciate your help. – K Venkat Sep 21 '17 at 14:57
0

You can extend the special file-descriptor technique to read from two files simultaneously and then mv.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76