Search code examples
bashshellawkrenamemv

Shell script calling rows from text document


I need some help with files renaming. At start I prepare text file : names.txt This file contain:

T22.tsv
T33.tsv
T101.tsv
T48.tsv

Names of files at start in the folder /home/filip/Desktop/

Xpress33.tsv
Xpress5.tsv
Xpress12.tsv
Xpress006.tsv

Names of files after mv at /home/filip/Desktop/:

T22.tsv
T33.tsv
T101.tsv
T48.tsv

Could you help, how could I read from the text file in bash script, it could be with awk. I tried :

A= awk 'NR==1 {print $0}' names.txt 
mv Xpress33.tsv   "$A"

But it doesn't work.


Solution

  • You want to store the output of a command into a variable. For this, you need the syntax var=$(command).

    Hence, this should make:

    A=$(awk 'NR==1 {print $0}' names.txt)
    mv Xpress33.tsv "$A"
    

    Note also that these are equivalent, because {print $0} is the default behaviour of awk:

    awk 'NR==1 {print $0}' names.txt
    awk 'NR==1' names.txt
    

    If you want to make it even more direct, you can do:

    mv Xpress33.tsv "$(awk 'NR==1' names.txt)"