[英]How to use awk/sed to replace data in a specific column in certain rows only
I have a space-delimited text file where the bulk of the rows look like this:我有一个以空格分隔的文本文件,其中大部分行如下所示:
--- 23:123456_A_B 123456 A B [and ~600K more columns after that]
However, there are some rows where the first 5 columns look like this:但是,有些行的前 5 列如下所示:
23 . 234567 C D
Can anyone suggest an awk or sed command (or any alternative method) that would change the first column from 23
to ---
and the second column from .
任何人都可以建议将第一列从23
更改为---
并将第二列从.
to 23:234567_C_D
(using the data from the first five columns) while leaving the other columns untouched, but only in those rows that start with 23
?到23:234567_C_D
(使用前五列的数据),同时保持其他列不变,但仅限于以23
开头的那些行?
Assuming the field separator is just a single space,假设字段分隔符只是一个空格,
sed -r 's/^23 \. ([^ ]+) ([^ ]+) ([^ ]+)/--- 23:\1_\2_\3 \1 \2 \3/' file
More readable with awk使用 awk 更具可读性
awk '$1 == 23 && $2 == "." {$1 = "---"; $2 = "23:" $3 "_" $4 "_" $5} 1' file
With sed, you can save the changes in-place with the -i
option.使用 sed,您可以使用-i
选项就地保存更改。
With awk, you may have to explicitly write to a temp file:使用 awk,您可能必须显式写入临时文件:
tmp=$(mktemp)
awk '....' file > "$tmp" && mv "$tmp" file
Is this what you want?这是你想要的吗?
$ cat file
--- 23:123456_A_B 123456 A B [and ~600K more columns after that]
23 . 234567 C D
$ awk '$1==23 { $2=$1":"$3"_"$4"_"$5; $1="---" }1' file
--- 23:123456_A_B 123456 A B [and ~600K more columns after that]
--- 23:234567_C_D 234567 C D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.