[英]interactive shell or bash script to manipulate a text file
I have a text file that contains 2 columns( example below ) 我有一个包含2列的文本文件(下面的示例)
Account_name Device_name
12345 1a3T567890f2
Values of the Device_name column then needs to be changed to: 然后需要将Device_name列的值更改为:
Uppercase letters if letters exist (example 1A3T567890F2
) 大写字母(如果存在字母)(示例
1A3T567890F2
)
awk '{ print toupper($0) }' file.txt > file2.txt
The Colon symbol needs to be inserted to separate the value in to 2 char chunks (example 1A:3T:56:78:90:F2
) 需要插入冒号符号以将值分成2个字符块(示例
1A:3T:56:78:90:F2
)
sed 's/\\(\\w\\w\\)\\(\\w\\w\\)\\(\\w\\w\\)\\(\\w\\w\\)\\(\\w\\w\\)\\(\\w\\w\\)/\\1:\\2:\\3:\\4:\\5:\\6/g' file2.txt > file3.txt
I would like to create a script that does those two functions at once. 我想创建一个同时执行这两个功能的脚本。
You can just add \\U
at the start of your sed's replace expression to switch the following to uppercase : 您只需在sed的replace表达式的开头添加
\\U
即可将以下内容切换为大写:
sed 's/(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)/\U\1:\2:\3:\4:\5:\6/g' file2.txt > file3.txt
Test run :
测试运行 :
$ echo "1a3T567890f2" | sed -r 's/(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)/\\U\\1:\\2:\\3:\\4:\\5:\\6/g' 1A:3T:56:78:90:F2
You can do everything in awk
: 你可以在
awk
做所有事情:
awk '{$2=toupper($2);gsub(/[[:alnum:]]{2}/,"&:", $2);sub(/:[[:space:]]*$/,"",$2)}1' file
That's a bit more intuitive and it works for various amount of digits. 这更加直观,并且适用于各种数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.