Linux one line command processing batch file details

  • 2021-07-03 01:08:50
  • OfStack


The best way is not the fastest you can think of. Scripts for temporary use in work are not required to be robust, and the sooner they are written, the better. Here to provide a use of sed command construction command to deal with batch file skills, for reference.

Requirements Case 1

Will all 0_80_91.txt, 0_80_92.txt, 0_80_93.txt in the current directory. . . The file names of 10 files are changed to 0_81_91.txt, 0_81_92.txt and 0_81_93.txt. That is, change 80 in the file name to 81.

The implementation command is: ls *.txt |sed -nr 's/(0_)(80)(.*)/mv \1\2\3 \181\3/gp' | sh

#ls *.txt 
0_80_91.txt 0_80_92.txt 0_80_93.txt
#ls *.txt |sed -nr 's/(0_)(80)(.*)/mv \1\2\3 \181\3/gp'
mv 0_80_91.txt 0_81_91.txt
mv 0_80_92.txt 0_81_92.txt
mv 0_80_93.txt 0_81_93.txt
#ls *.txt |sed -nr 's/(0_)(80)(.*)/mv \1\2\3 \181\3/gp' | sh
#ls *.txt
0_81_91.txt 0_81_92.txt 0_81_93.txt

Requirements Case 2

Extract all 0_80_91.Z, 0_80_92.Z, 0_80_93.Z files in the current directory by calling the command cc_uncompress and output them to the specified files. The call format is cc_uncompress-s 0_80_91.txt-d 1.txt. 1. txt can be any file name.

Implement Command 1 as: ls *.Z | sed -nr 's/(.*)/cc_uncompress -s \1 -d \1.txt/gp'

#ls *.Z | sed -nr 's/(.*)/cc_uncompress -s \1 -d \1.txt/gp'
cc_uncompress -s 0_80_91.Z -d 0_80_91.Z.txt
cc_uncompress -s 0_80_92.Z -d 0_80_92.Z.txt
cc_uncompress -s 0_80_93.Z -d 0_80_93.Z.txt
#ls *.Z | sed -nr 's/(.*)/cc_uncompress -s \1 -d \1.txt/gp' | sh

Implement Command 2 as: find . -name "*.Z" -exec cc_uncompress -s {} -d {}.bak \;

Related articles: