Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个文本文件,其中包含 MM/DD/YYYY 格式的数千个随机日期。我需要将所有这些日期转换为 YY/MM/DD。使用 bash 最快和最简单的方法是什么?请注意,我们需要搜索日期,因为该文件还包含其他文本内容。
甚至不是很强大,但您可以尝试:
perl -pe 's@\b(\d{1,2})/(\d{1,2})/\d\d(\d\d)\b@$3/$1/$2@' input
与sed:
sed
sed -i 's/\([0-9][0-9]\)\/\([0-9][0-9]\)\/[0-9][0-9]\([0-9][0-9]\)/\3\/\1\/\2/g' your_text_file
如果您想在修改之前备份文件,请使用-i.bak而不是。-i
-i.bak
-i