AWK-OFS无法使用FS字符集 [英] AWK - OFS is not working with FS character set
问题描述
是否存在任何使用awk的人至少会因OFS失败一次的咒语?
Is there any spell that anyone who uses awk will fail with OFS at least once?
我有以下文件:
someone.txt
LN_A,FN_A<aa@xyz.com>;
LN_B,FN_B<bb@xyz.com>;
而且,我需要:
FN_A,LN_A,aa
FN_B,LN_B,bb
最初,我尝试使用以下输出进行以下操作:
Initially, I tried the following with the below output:
awk -F'[,<@]' -v OFS=',' '{print $2 $1 $3}' someone.txt
FN_ALN_Aaa
FN_BLN_Bbb
经过一些研究,我发现有时需要重建记录.因此,我尝试以下操作,假设更改NF可以重建记录.
After doing some research, I found that sometimes the record needs to be reconstructed. So, I tried the following, assuming changing NF will reconstruct the record.
awk -F'[,<@]' -v OFS=',' 'NF=3 {print $2 $1 $3}' someone.txt
FN_ALN_Aaa
FN_BLN_Bbb
然后,我尝试了此操作:
Then, I tried this:
awk -F'[,<@]' -v OFS=',' 'NF=3; {print $2 $1 $3}' someone.txt
LN_A,FN_A,aa
FN_ALN_Aaa
LN_B,FN_B,bb
FN_BLN_Bbb
然后,这可以重建记录,仍然没有运气:
Then, this to reconstruct the record, still no luck:
awk -F'[,<@]' -v OFS=',' '{$1=$1} {print $2 $1 $3}' someone.txt
FN_ALN_Aaa
FN_BLN_Bbb
这是可行的,但没有达到预期效果:
This one worked, but not as expected:
awk -F'[,<@]' -v OFS=',' '{$1=$1} {print $0}' someone.txt
LN_A,FN_A,aa,xyz.com>;
LN_B,FN_B,bb,xyz.com>;
最后,我通过以下方法进行管理:
Finally, I managed with the following:
awk -F'[,<@]' '{print $2 "," $1 "," $3}' someone.txt
FN_A,LN_A,aa
FN_B,LN_B,bb
PS:
- 我知道同一行上有很多问题,但是我不认为这是重复的,因为它不起作用-至少对我来说. :-)
- 我正在使用GNU Awk 3.1.7
推荐答案
OFS
是在print
吃了原始(未引号)逗号时生成的
OFS
is generated when print
eats a raw (unquoted) comma
我会这样做:
$ awk -F'[,<@]' -v OFS=, '{print $2,$1,$3}' someone.txt
FN_A,LN_A,aa
FN_B,LN_B,bb
这篇关于AWK-OFS无法使用FS字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!