简体   繁体   English

bash 脚本:如何在 .<dot> 对于文件中的每一行

[英]bash scripting: how to move a string before .<dot> for each line in a file

file contains:文件包含:

compat-db.x86_64                      4.6.21-17.el6                          
chkconfig.x86_64                      1.3.49.5-1.el6

I would like to add prefix - before 4.6.21-17.el6 and move this before .我想在4.6.21-17.el6之前添加前缀-并将其移动到. with bash scripting.使用 bash 脚本。

So the output should be:所以输出应该是:

compat-db-4.6.21-17.el6.x86_64
chkconfig-1.3.49.5-1.el6.x86_64

Any suggestions how to achieve this?任何建议如何实现这一目标?

awk solution: awk解决方法:

awk '{ sub(/\./,"-"$2".",$1); print $1 }' file

The output:输出:

compat-db-4.6.21-17.el6.x86_64
chkconfig-1.3.49.5-1.el6.x86_64

GNU-sed solution: GNU-sed 解决方案:

sed 's/\.\(.*\)[[:space:]]\+\(.*\)/-\2.\1/'
  • \\. matches a dot literally字面上匹配一个点
  • [:space:] is a POSIX class, in a character class, it matches any whitespace character [:space:]是一个 POSIX 类,在字符类中,它匹配任何空白字符
  • \\+ means "at least once" \\+表示“至少一次”
  • \\(...\\) is used for capturing \\(...\\)用于捕获

BSD Sed & GNU 解决方案:

sed -E 's!^([^\s\.]*)(\.[a-Z0-9_^\s]*)[\s]*([^\s]*)[\s]*$!\1-\3\2!g;s! !!g' < yourfile.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量中的字符串添加到BASH文件中的每一行 - How to add a string from variables to each line in a file in BASH 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file? Bash Scripting:如果行以(或匹配)另一个字符串开头,则替换(或删除)文件中的字符串 - Bash Scripting: Replace (or delete) string in a file if line starts with (or matches) another string 如何使用基本的 linux 命令计算每行中的点数,然后在 bash 中的每行中插入值 - how to use basic linux command to count the number of dot in every line then insert the value in each line in bash 如何逐行读取文件并将每行重定向到Shell脚本中的变量? - How can I read a file line by line and redirect each line to a variable in shell scripting? 如何使用Bash将字符串添加到文件中的每一行 - How to add a string to every line in a file with Bash 如何在bash脚本中求和不同文件的列 - How to sum column of different file in bash scripting Bash脚本:为什么此文件中缺少最后一行? - Bash scripting: why is the last line missing from this file append? 围绕nc命令编写的bash脚本。 如何在每行之前添加文字? - A bash script written around the nc command. How do I prepend text before each line? 如何使用bash为每个命名的hosts文件添加一个字符串 - how to use bash to add a string to each named hosts file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM