Help with naming the file

Hi,

I have a folder that contains files

abc.txt
def.txt
....and so on

Inside abc.txt, I have

@<TRIPOS>MOLECULE
4|Chelerythrine|abcb11_earlyIdentification_Stronginhib_washed_ligprep|sdf|1|dock

Inside def.txt, I have

@<TRIPOS>MOLECULE
160|Tipranavir|abcb11_earlyIdentification_Stronginhib_washed_ligprep|sdf|17|dock

I want to rename

abc.txt to abc.txt4
def.txt to def.txt160

Can someone help me with this.

Thanks!!!

Try

for file in *.txt
do
  { read; IFS=\| read nr rest;} < "$file"
  if [ "$nr" ]; then
     mv "$file" "${file}${nr}"
  fi
done
2 Likes

Hello rossi,

A few questions to clarity thing first:-

  • Is there anything else in the file apart from these two lines?
  • Can I just confirm that you want to suffix the file name with the first field of the second line using a pipe | as the delimeter?
  • Are there many, many files?
  • What have you tried so far?
  • What errors have you got?
  • What shell are you using?
  • What OS and version do you have running?
  • What tools are you most comfortable with?
  • Does the code from Scrutinizer solve it?

Sorry it's so many, but i don't want anyone to shoot off on the wrong track.

Kind regards,
Robin

Thanks!!!
The code works fro this problem.
Can you help in solving this issue
suppose the file name is

abc7634.txt
def87.txt
kslkd836990.txt

Assuming that kslkd has 12 inside the file

I want the output like

abc4
def160
kslkd12

Thanks!!

So drop the .txt then?

Try this:-

for file in *.txt
do
  { read; IFS=\| read nr rest;} < "$file"
  if [ "$nr" ]; then
     mv "$file" "${file%.txt}${nr}"
  fi
done

Hi rabtte1

The code from Scrutinizer works for the problem I posted.

  1. Yes there are more lines in the file, but I want the number just from the 2nd line.
  2. I want the number before the | delimiter
  3. There are about 100 files
  4. I tried python but couldn't solve the issue

I have to drop the numbers before *.txt as well
Regards
Vishal

Try - in the code snippets posted above - this:

mv "$file" "${file%%[0-9]*}${nr}"