开发者

How to rename text files according to their content?

My OS is Ubuntu; I have a directory with text files; each file includes a line with unique ID

IDtag IDnumber

The line location is random inside a text file, however, it is easy to recognize it be simple regex

^IDtag ID[0-9]*

How to rename the files according to their IDs? I.e. each file has to obtain a new name IDnumber.txt I can run a Linux shel开发者_Go百科l script or Perl script.


Try doing this first

for i in a b c; do echo mv $i `egrep -m1 -e 'IDtag ID[0-9]*' $i | sed -e 's/IDtag ID//'`; done

If the mvs are correct - remove the echo bit


Sounds like a homework. But i would partly answer your question. below will give the list of all command to be executed to rename the files.

grep ^IDtag * | awk '{split($1,a,":");print "mv",a[1], $2".txt"}'

ivory.192> grep ^IDtag * | awk '{split($1,a,":");print "mv",a[1], $2".txt"}'
mv 000001 000001.txt
mv temp2 000002.txt
ivory.193> 

put this in a for loop and execute each and every line in a shell script.

additionally to have a track you can redirect the output to a file and give the file execute permissions and execute it as a script.this will do your purpose additionally you also have the list of files which were renamed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜