在shell脚本中填充字符串/文件名

时间:2013-05-28 18:17:43

标签: shell

我正在制作编号为1-1000的文件。我需要打印文件,使第一个0000.txt第二个 0001.txt百分之一和第一个将是0100.txt等。

什么命令可以完成与c?

中的printf格式%04d相同的操作

编辑:我在Mac OSX上运行所有内容,并在终端中使用bash。太公平了我不知道POSIX是什么,甚至是什么shell。对不起,我之前没有指定我的系统。

2 个答案:

答案 0 :(得分:27)

使用printf,就像在C中一样。它可用于任何POSIXly Bourne Shell(zsh,ksh,bash,...)

$ FILE=$(printf %04d 42).txt
$ echo "$FILE"
0042.txt

答案 1 :(得分:8)

您可以尝试这样的事情:

for i in $(seq -w 0 1000); do 
    touch "$i".txt; 
done

测试:

$ mkdir test
$ cd test
$ ls
$ for i in $(seq -w 0 1000); do
> touch "$i".txt;
> done
$ ls
0000.txt  0044.txt  0088.txt  0132.txt  0176.txt  0220.txt  0264.txt  0308.txt  0352.txt  0396.txt  0440.txt  0484.txt  0528.txt  0572.txt  0616.txt  0660.txt  0704.txt  0748.txt  0792.txt  0836.txt  0880.txt  0924.txt  0968.txt
0001.txt  0045.txt  0089.txt  0133.txt  0177.txt  0221.txt  0265.txt  0309.txt  0353.txt  0397.txt  0441.txt  0485.txt  0529.txt  0573.txt  0617.txt  0661.txt  0705.txt  0749.txt  0793.txt  0837.txt  0881.txt  0925.txt  0969.txt
0002.txt  0046.txt  0090.txt  0134.txt  0178.txt  0222.txt  0266.txt  0310.txt  0354.txt  0398.txt  0442.txt  0486.txt  0530.txt  0574.txt  0618.txt  0662.txt  0706.txt  0750.txt  0794.txt  0838.txt  0882.txt  0926.txt  0970.txt
0003.txt  0047.txt  0091.txt  0135.txt  0179.txt  0223.txt  0267.txt  0311.txt  0355.txt  0399.txt  0443.txt  0487.txt  0531.txt  0575.txt  0619.txt  0663.txt  0707.txt  0751.txt  0795.txt  0839.txt  0883.txt  0927.txt  0971.txt
0004.txt  0048.txt  0092.txt  0136.txt  0180.txt  0224.txt  0268.txt  0312.txt  0356.txt  0400.txt  0444.txt  0488.txt  0532.txt  0576.txt  0620.txt  0664.txt  0708.txt  0752.txt  0796.txt  0840.txt  0884.txt  0928.txt  0972.txt
0005.txt  0049.txt  0093.txt  0137.txt  0181.txt  0225.txt  0269.txt  0313.txt  0357.txt  0401.txt  0445.txt  0489.txt  0533.txt  0577.txt  0621.txt  0665.txt  0709.txt  0753.txt  0797.txt  0841.txt  0885.txt  0929.txt  0973.txt
0006.txt  0050.txt  0094.txt  0138.txt  0182.txt  0226.txt  0270.txt  0314.txt  0358.txt  0402.txt  0446.txt  0490.txt  0534.txt  0578.txt  0622.txt  0666.txt  0710.txt  0754.txt  0798.txt  0842.txt  0886.txt  0930.txt  0974.txt
0007.txt  0051.txt  0095.txt  0139.txt  0183.txt  0227.txt  0271.txt  0315.txt  0359.txt  0403.txt  0447.txt  0491.txt  0535.txt  0579.txt  0623.txt  0667.txt  0711.txt  0755.txt  0799.txt  0843.txt  0887.txt  0931.txt  0975.txt
0008.txt  0052.txt  0096.txt  0140.txt  0184.txt  0228.txt  0272.txt  0316.txt  0360.txt  0404.txt  0448.txt  0492.txt  0536.txt  0580.txt  0624.txt  0668.txt  0712.txt  0756.txt  0800.txt  0844.txt  0888.txt  0932.txt  0976.txt
0009.txt  0053.txt  0097.txt  0141.txt  0185.txt  0229.txt  0273.txt  0317.txt  0361.txt  0405.txt  0449.txt  0493.txt  0537.txt  0581.txt  0625.txt  0669.txt  0713.txt  0757.txt  0801.txt  0845.txt  0889.txt  0933.txt  0977.txt
0010.txt  0054.txt  0098.txt  0142.txt  0186.txt  0230.txt  0274.txt  0318.txt  0362.txt  0406.txt  0450.txt  0494.txt  0538.txt  0582.txt  0626.txt  0670.txt  0714.txt  0758.txt  0802.txt  0846.txt  0890.txt  0934.txt  0978.txt
0011.txt  0055.txt  0099.txt  0143.txt  0187.txt  0231.txt  0275.txt  0319.txt  0363.txt  0407.txt  0451.txt  0495.txt  0539.txt  0583.txt  0627.txt  0671.txt  0715.txt  0759.txt  0803.txt  0847.txt  0891.txt  0935.txt  0979.txt
0012.txt  0056.txt  0100.txt  0144.txt  0188.txt  0232.txt  0276.txt  0320.txt  0364.txt  0408.txt  0452.txt  0496.txt  0540.txt  0584.txt  0628.txt  0672.txt  0716.txt  0760.txt  0804.txt  0848.txt  0892.txt  0936.txt  0980.txt
0013.txt  0057.txt  0101.txt  0145.txt  0189.txt  0233.txt  0277.txt  0321.txt  0365.txt  0409.txt  0453.txt  0497.txt  0541.txt  0585.txt  0629.txt  0673.txt  0717.txt  0761.txt  0805.txt  0849.txt  0893.txt  0937.txt  0981.txt
0014.txt  0058.txt  0102.txt  0146.txt  0190.txt  0234.txt  0278.txt  0322.txt  0366.txt  0410.txt  0454.txt  0498.txt  0542.txt  0586.txt  0630.txt  0674.txt  0718.txt  0762.txt  0806.txt  0850.txt  0894.txt  0938.txt  0982.txt
0015.txt  0059.txt  0103.txt  0147.txt  0191.txt  0235.txt  0279.txt  0323.txt  0367.txt  0411.txt  0455.txt  0499.txt  0543.txt  0587.txt  0631.txt  0675.txt  0719.txt  0763.txt  0807.txt  0851.txt  0895.txt  0939.txt  0983.txt
0016.txt  0060.txt  0104.txt  0148.txt  0192.txt  0236.txt  0280.txt  0324.txt  0368.txt  0412.txt  0456.txt  0500.txt  0544.txt  0588.txt  0632.txt  0676.txt  0720.txt  0764.txt  0808.txt  0852.txt  0896.txt  0940.txt  0984.txt
0017.txt  0061.txt  0105.txt  0149.txt  0193.txt  0237.txt  0281.txt  0325.txt  0369.txt  0413.txt  0457.txt  0501.txt  0545.txt  0589.txt  0633.txt  0677.txt  0721.txt  0765.txt  0809.txt  0853.txt  0897.txt  0941.txt  0985.txt
0018.txt  0062.txt  0106.txt  0150.txt  0194.txt  0238.txt  0282.txt  0326.txt  0370.txt  0414.txt  0458.txt  0502.txt  0546.txt  0590.txt  0634.txt  0678.txt  0722.txt  0766.txt  0810.txt  0854.txt  0898.txt  0942.txt  0986.txt
0019.txt  0063.txt  0107.txt  0151.txt  0195.txt  0239.txt  0283.txt  0327.txt  0371.txt  0415.txt  0459.txt  0503.txt  0547.txt  0591.txt  0635.txt  0679.txt  0723.txt  0767.txt  0811.txt  0855.txt  0899.txt  0943.txt  0987.txt
0020.txt  0064.txt  0108.txt  0152.txt  0196.txt  0240.txt  0284.txt  0328.txt  0372.txt  0416.txt  0460.txt  0504.txt  0548.txt  0592.txt  0636.txt  0680.txt  0724.txt  0768.txt  0812.txt  0856.txt  0900.txt  0944.txt  0988.txt
0021.txt  0065.txt  0109.txt  0153.txt  0197.txt  0241.txt  0285.txt  0329.txt  0373.txt  0417.txt  0461.txt  0505.txt  0549.txt  0593.txt  0637.txt  0681.txt  0725.txt  0769.txt  0813.txt  0857.txt  0901.txt  0945.txt  0989.txt
0022.txt  0066.txt  0110.txt  0154.txt  0198.txt  0242.txt  0286.txt  0330.txt  0374.txt  0418.txt  0462.txt  0506.txt  0550.txt  0594.txt  0638.txt  0682.txt  0726.txt  0770.txt  0814.txt  0858.txt  0902.txt  0946.txt  0990.txt
0023.txt  0067.txt  0111.txt  0155.txt  0199.txt  0243.txt  0287.txt  0331.txt  0375.txt  0419.txt  0463.txt  0507.txt  0551.txt  0595.txt  0639.txt  0683.txt  0727.txt  0771.txt  0815.txt  0859.txt  0903.txt  0947.txt  0991.txt
0024.txt  0068.txt  0112.txt  0156.txt  0200.txt  0244.txt  0288.txt  0332.txt  0376.txt  0420.txt  0464.txt  0508.txt  0552.txt  0596.txt  0640.txt  0684.txt  0728.txt  0772.txt  0816.txt  0860.txt  0904.txt  0948.txt  0992.txt
0025.txt  0069.txt  0113.txt  0157.txt  0201.txt  0245.txt  0289.txt  0333.txt  0377.txt  0421.txt  0465.txt  0509.txt  0553.txt  0597.txt  0641.txt  0685.txt  0729.txt  0773.txt  0817.txt  0861.txt  0905.txt  0949.txt  0993.txt
0026.txt  0070.txt  0114.txt  0158.txt  0202.txt  0246.txt  0290.txt  0334.txt  0378.txt  0422.txt  0466.txt  0510.txt  0554.txt  0598.txt  0642.txt  0686.txt  0730.txt  0774.txt  0818.txt  0862.txt  0906.txt  0950.txt  0994.txt
0027.txt  0071.txt  0115.txt  0159.txt  0203.txt  0247.txt  0291.txt  0335.txt  0379.txt  0423.txt  0467.txt  0511.txt  0555.txt  0599.txt  0643.txt  0687.txt  0731.txt  0775.txt  0819.txt  0863.txt  0907.txt  0951.txt  0995.txt
0028.txt  0072.txt  0116.txt  0160.txt  0204.txt  0248.txt  0292.txt  0336.txt  0380.txt  0424.txt  0468.txt  0512.txt  0556.txt  0600.txt  0644.txt  0688.txt  0732.txt  0776.txt  0820.txt  0864.txt  0908.txt  0952.txt  0996.txt
0029.txt  0073.txt  0117.txt  0161.txt  0205.txt  0249.txt  0293.txt  0337.txt  0381.txt  0425.txt  0469.txt  0513.txt  0557.txt  0601.txt  0645.txt  0689.txt  0733.txt  0777.txt  0821.txt  0865.txt  0909.txt  0953.txt  0997.txt
0030.txt  0074.txt  0118.txt  0162.txt  0206.txt  0250.txt  0294.txt  0338.txt  0382.txt  0426.txt  0470.txt  0514.txt  0558.txt  0602.txt  0646.txt  0690.txt  0734.txt  0778.txt  0822.txt  0866.txt  0910.txt  0954.txt  0998.txt
0031.txt  0075.txt  0119.txt  0163.txt  0207.txt  0251.txt  0295.txt  0339.txt  0383.txt  0427.txt  0471.txt  0515.txt  0559.txt  0603.txt  0647.txt  0691.txt  0735.txt  0779.txt  0823.txt  0867.txt  0911.txt  0955.txt  0999.txt
0032.txt  0076.txt  0120.txt  0164.txt  0208.txt  0252.txt  0296.txt  0340.txt  0384.txt  0428.txt  0472.txt  0516.txt  0560.txt  0604.txt  0648.txt  0692.txt  0736.txt  0780.txt  0824.txt  0868.txt  0912.txt  0956.txt  1000.txt
0033.txt  0077.txt  0121.txt  0165.txt  0209.txt  0253.txt  0297.txt  0341.txt  0385.txt  0429.txt  0473.txt  0517.txt  0561.txt  0605.txt  0649.txt  0693.txt  0737.txt  0781.txt  0825.txt  0869.txt  0913.txt  0957.txt
0034.txt  0078.txt  0122.txt  0166.txt  0210.txt  0254.txt  0298.txt  0342.txt  0386.txt  0430.txt  0474.txt  0518.txt  0562.txt  0606.txt  0650.txt  0694.txt  0738.txt  0782.txt  0826.txt  0870.txt  0914.txt  0958.txt
0035.txt  0079.txt  0123.txt  0167.txt  0211.txt  0255.txt  0299.txt  0343.txt  0387.txt  0431.txt  0475.txt  0519.txt  0563.txt  0607.txt  0651.txt  0695.txt  0739.txt  0783.txt  0827.txt  0871.txt  0915.txt  0959.txt
0036.txt  0080.txt  0124.txt  0168.txt  0212.txt  0256.txt  0300.txt  0344.txt  0388.txt  0432.txt  0476.txt  0520.txt  0564.txt  0608.txt  0652.txt  0696.txt  0740.txt  0784.txt  0828.txt  0872.txt  0916.txt  0960.txt
0037.txt  0081.txt  0125.txt  0169.txt  0213.txt  0257.txt  0301.txt  0345.txt  0389.txt  0433.txt  0477.txt  0521.txt  0565.txt  0609.txt  0653.txt  0697.txt  0741.txt  0785.txt  0829.txt  0873.txt  0917.txt  0961.txt
0038.txt  0082.txt  0126.txt  0170.txt  0214.txt  0258.txt  0302.txt  0346.txt  0390.txt  0434.txt  0478.txt  0522.txt  0566.txt  0610.txt  0654.txt  0698.txt  0742.txt  0786.txt  0830.txt  0874.txt  0918.txt  0962.txt
0039.txt  0083.txt  0127.txt  0171.txt  0215.txt  0259.txt  0303.txt  0347.txt  0391.txt  0435.txt  0479.txt  0523.txt  0567.txt  0611.txt  0655.txt  0699.txt  0743.txt  0787.txt  0831.txt  0875.txt  0919.txt  0963.txt
0040.txt  0084.txt  0128.txt  0172.txt  0216.txt  0260.txt  0304.txt  0348.txt  0392.txt  0436.txt  0480.txt  0524.txt  0568.txt  0612.txt  0656.txt  0700.txt  0744.txt  0788.txt  0832.txt  0876.txt  0920.txt  0964.txt
0041.txt  0085.txt  0129.txt  0173.txt  0217.txt  0261.txt  0305.txt  0349.txt  0393.txt  0437.txt  0481.txt  0525.txt  0569.txt  0613.txt  0657.txt  0701.txt  0745.txt  0789.txt  0833.txt  0877.txt  0921.txt  0965.txt
0042.txt  0086.txt  0130.txt  0174.txt  0218.txt  0262.txt  0306.txt  0350.txt  0394.txt  0438.txt  0482.txt  0526.txt  0570.txt  0614.txt  0658.txt  0702.txt  0746.txt  0790.txt  0834.txt  0878.txt  0922.txt  0966.txt