如何在命令行中用M和F替换1或2(How to replace 1 or 2 with M and F in command line)
我有一个包含四列(四个字段)的文件。 一列的性别编码为1或2.我如何使用awk命令将M替换为1,将F替换为2?
I have a file with four columns( four fields). One column is sex coded as 1 or 2. How could I use awk command to replace 1 by M and 2 by F?
原文:
更新时间:2022-12-01 10:12
最满意答案
尝试以下中间比赛:
\s*(?:\S\s*){10,100}
正则表达式:
\s* whitespace (\n, \r, \t, \f, and " ") (0 or more times) (?: group, but do not capture (between 10 and 100 times) \S non-whitespace (all but \n, \r, \t, \f, and " ") \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times) ){10,100} end of grouping
所以你可以从表达式开始......
(\d{2,5}|post office|p[\. ]?o\.?)(\s*(?:\S\s*){10,100})
现场演示您的数据。
Try the following for your middle match:
\s*(?:\S\s*){10,100}
Regular expression:
\s* whitespace (\n, \r, \t, \f, and " ") (0 or more times) (?: group, but do not capture (between 10 and 100 times) \S non-whitespace (all but \n, \r, \t, \f, and " ") \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times) ){10,100} end of grouping
So you could start off your expression something like..
(\d{2,5}|post office|p[\. ]?o\.?)(\s*(?:\S\s*){10,100})
Live demo with your data.
相关问答
更多-
这是一个适合你的正则表达式: \bOKAY(?>-?)(\w)?([^\w\s]\S*)?(?!\S) 由于您不清楚您使用的是哪种语言,因此这里是伪代码,用于替换您的方式。 "O" + (match.group(1) if match.group(1) else "K") + match.group(2) 这是一个小问题: http ://www.rubular.com/r/SE8MBkUUUo 编辑:我在评论后对上述正则表达式进行了一些更改,但下面的描述并未反映这些更改。 以下是原始正则表达式的更改: ...
-
试试这个 : ^/wp-content/themes/(?!mythemename).*/style.css$ 演示: http : //regexr.com?30ote 提示:使用否定前瞻断言 。 Try this : ^/wp-content/themes/(?!mythemename).*/style.css$ Demo : http://regexr.com?30ote Hint : Using Negative look-ahead assertion.
-
从Kernel#global_variables返回的编号变量将始终保持不变,即使在分配之前。 即使在比赛之前,也就是$1 $9都会被退回,而且更多的比赛不会添加到列表中。 (他们也不能被分配,例如使用$10 = "foo" 。) 考虑该方法的源代码: VALUE rb_f_global_variables(void) { VALUE ary = rb_ary_new(); char buf[2]; int i; st_foreach_safe(rb_global_tbl, ...
-
匹配Unicode字符串的正则表达式与至少一个空格(Match a regular expression for a Unicode string with at least one space)[2022-03-15]
这可能有用,但没有测试过。 编辑:嗯,经过测试/调试,这就是我得到的,祝你好运! # ^(?=[\pL\pN\s'\-.]+$)(?=[^\pL\pN]*(?:[\pL\pN][^\pL\pN]*){4,}$)(?![\pN'\-.])(?=\S+\s+\S) ^ (?= [\pL\pN\s'\-.]+ $ ) # Allowed characters (?= # At least 4 alphanumeric chars ... -
尝试这个: ^(\d\s*){6}$ 它允许在每个数字后面有0个或更多的空白字符。 如果要将空格限制在数字内(不带前导或尾随空格): ^(\d\s*){5}\d$ Try this: ^(\d\s*){6}$ It allows 0 or more whitespace characters after every digit. If you want to limit whitespace to be inside the digits (without leading or trailing sp ...
-
正则表达式匹配之前已解析过的数量的字符(Regular expression matching a number of characters which amount had been parsed before)[2021-09-26]
正则表达式是不可能的。 ([0-9]*)只是“记住”数字作为子字符串:它们不能以数字方式使用。 That's not possible with regex. ([0-9]*) just "remembers" the digits as a substring: they can't be used numerically. -
如何使用正则表达式搜索具有不同数量的空格的模式?(How do I search for a pattern with different numbers of white spaces using regex?)[2022-09-14]
在您链接的示例中,开头和结尾的斜杠是模式的一部分,因此它们与答案的正则表达式部分无关。 如果我理解你的问题, pattern将始终是连续的。 在这种情况下,它后面有多少空白并不重要,你总能找到它。 如果你想确保在捕获那个空格时找到pattern ,请使用类似的东西 import re lines = 'hello\nhello \n' pattern = 'hello' results = re.findall(pattern + r'\s*\n', lines) print(results) >> ... -
正则表达式匹配字符串中任意位置的一定数量的数字(Regular expression to match a certain number of digits anywhere in a string)[2022-07-29]
要在带有正则表达式的字符串中只允许10位数,不能再少,可以使用 ^(?:\D*\d){10}\D*$ 请参阅演示 ( \D替换为[^\d\n]以排除仅用于演示的换行符)。 (\d\D*?){10}正则表达式将在超过10位数的字符串中找到匹配项。 To allow just 10 digits in a string with a regex, no more no less, you can use ^(?:\D*\d){10}\D*$ See demo (\D replaced with [^\d\n ... -
尝试以下中间比赛: \s*(?:\S\s*){10,100} 正则表达式: \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times) (?: group, but do not capture (between 10 and 100 times) \S non-whitespace (all but \n, \r, \t, \f, and " ") \s* ...
-
这个正则表达式找到以left开头并以right结尾的最短匹配,并允许其间的任何文本: (left)(?:(?!(?1)).)*?(?:right) 这个正则表达式适用于PCRE和支持子程序调用的任何其他风格。 left和right位不必是文字文本。 它们可以是任何正则表达式,只要它们与相同的文本不匹配即可。 您需要考虑的唯一其他问题是,如果left和right包含捕获组,那么这些组的数量将作为独立的正则表达式left和right移动。 回溯正则表达式引擎总是返回最左边的匹配。 在right不同的情况下,我 ...