用awk替换字符串中的字符或sed的 [英] Replace character in a string using awk or sed
问题描述
,我怎么可以通过获取第14和15位用awk或更换第六和第七的sed。
Given the string below, how can I replace 6th and 7th by getting 14th and 15th digit using awk or sed.
xxxxx02xxxxxx89xx
xxxxx22xxxxxx33xx
输出
xxxxx89xxxxxx89xx
xxxxx33xxxxxx33xx
我是新手在这里,对不起我的问题。
I am newbie here, sorry for my question.
推荐答案
sed命令很简单,但难以阅读:
The sed command is straightforward, but hard to read:
sed 's/\(.....\)..\(......\)\(..\)/\1\3\2\3/'
有一个可能更易于维护的解决方案可与牛羚可以了 AWK
(而不是其他的awk品种然而,见下文。)
A possibly more maintainable solution can be had with Gnu awk
(but not other awk varieties. However, see below.):
gawk -v FIELDWIDTHS="5 2 6 2 999" -v OFS='' '{$2=$4;print}'
的 FIELDWIDTHS
变量定义5个固定宽度的字段:前5个字符,接下来的两个字符(位置6和7);接下来的六个字符(8〜13);接下来的两个字符(14和15);和下一个(最多)999个字符,这应该是该行的其余部分。 (如果您有较长的线条,增加必要的)。设置 OFS
来的空往往是具有固定长度字段有用;它prevents AWK
从输出字段之间插入空格。
The FIELDWIDTHS
variable defines 5 fixed-width fields: the first 5 characters, the next two characters (positions 6 and 7); the next six characters (8 through 13); the next two characters (14 and 15); and the next (up to) 999 characters, which should be the rest of the line. (If you have longer lines, increase as necessary). Setting OFS
to empty is often useful with fixed-length fields; it prevents awk
from inserting spaces between fields in the output.
FIELDWIDTHS
是一个GNU awk的扩展。然而,这是很容易在Posix的 AWK
重新实现。这里有一个简单的实现:
FIELDWIDTHS
is a GNU awk extension. However, it is easy enough to reimplement in Posix awk
. Here's a simple implementation:
function fieldwidth_set( i) {
if (PROCINFO["FS"]) FIELDWIDTHS = FIELDWIDTHS;
else if (length(FIELDWIDTHS)) {
_FW_NF = split(FIELDWIDTHS, _FW_ARRAY);
for (i in _FW_ARRAY) {
if (_FW_ARRAY[i] !~ /^[0-9]+$/) {
printf "Illegal value '%s' in FIELDWIDTHS\n", _FW_ARRAY[i] >>"/dev/stderr";
exit 1;
}
_FW_ARRAY[i]+=0;
}
} else
_FW_NF = 0;
}
function set_fieldwidth(fw) { FIELDWIDTHS=fw; fieldwidth_set(); }
function fw_( a,i,k) {
if (_FW_NF) {
a = $0;
$0 = "";
k=1;
for (i=1; i<=_FW_NF; ++i) {
$i = substr(a, k, _FW_ARRAY[i]);
k+=_FW_ARRAY[i];
}
}
}
BEGIN{set_fieldwidth()}
{fw_()}
据我所知,只有了GNU AWK,您可以在 AWK
命令行程序混合文件和程序文本。 POSIX要求在 -f程序文件
选项,它可以重复,但不要求 -e程序文本
通过了GNU AWK实现的选项。因此,如果你想使用上面的代码中有一个命令行awk程序,你需要做的是这样的:
As far as I know, only Gnu awk lets you mix program files and program text on the awk
command-line. Posix requires the -f program-file
option, which may be repeated, but does not require the -e program-text
option as implemented by Gnu awk. Consequently, if you want to use the above snippet with a command-line awk program, you need to do something like this:
awk -v FIELDWIDTHS="5 2 6 2 999" -v OFS= -f fw.awk -f <(echo '{$2=$4;print}')
(假设你把fieldwidth片段到 fw.awk
)
为了提高效率, fw.awk
坚持,你告诉它你已经修改 FIELDWIDTHS
致电 fieldwidth_set()
。或者你可以使用 set_fieldwidth(......)
到 FIELDWIDTHS
设置为一个新值。它将与GNU AWK以及与其他AWK实现工作;它可以让GNU AWK做繁重。
For efficiency, fw.awk
insists that you tell it that you've changed FIELDWIDTHS
by calling fieldwidth_set()
. Or you can use set_fieldwidth("....")
to set FIELDWIDTHS
to a new value. It will work with GNU awk as well as with other awk implementations; it lets GNU awk do the heavy lifting.
这篇关于用awk替换字符串中的字符或sed的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!