如何使用 AWK 反转字段顺序?

发布于 2024-09-12 07:30:18 字数 221 浏览 3 评论 0原文

我有一个具有以下布局的文件:

123,01-08-2006
124,01-09-2007
125,01-10-2009
126,01-12-2010

如何使用 AWK 将其转换为以下格式?

123,2006-08-01
124,2007-09-01
125,2009-10-01
126,2009-12-01

I have a file with the following layout:

123,01-08-2006
124,01-09-2007
125,01-10-2009
126,01-12-2010

How can I convert it into the following by using AWK?

123,2006-08-01
124,2007-09-01
125,2009-10-01
126,2009-12-01

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羅雙樹 2024-09-19 07:30:18

第一次没有正确阅读问题。您需要一个字段分隔符,可以是破折号或逗号。一旦你有了,你可以使用破折号作为输出字段分隔符(因为它是最常见的)并使用连接来伪造逗号:

awk -F',|-' 'OFS="-" {print $1 "," $4,$3,$2}' file

Didn't read the question properly the first time. You need a field separator that can be either a dash or a comma. Once you have that you can use the dash as an output field separator (as it's the most common) and fake the comma using concatenation:

awk -F',|-' 'OFS="-" {print $1 "," $4,$3,$2}' file
强辩 2024-09-19 07:30:18

纯 awk

awk -F"," '{ n=split($2,b,"-");$2=b[3]"-"b[2]"-"b[1];$i=$1","$2 } 1' file

sed

sed -r 's/(^.[^,]*,)([0-9]{2})-([0-9]{2})-([0-9]{4})/\1\4-\3-\2/' file
sed 's/\(^.[^,]*,\)\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9]\+\)/\1\4-\3-\2/' file

Bash

#!/bin/bash

while IFS="," read -r a b
do
  IFS="-"
  set -- $b
  echo "$a,$3-$2-$1"
done <"file"

Pure awk

awk -F"," '{ n=split($2,b,"-");$2=b[3]"-"b[2]"-"b[1];$i=$1","$2 } 1' file

sed

sed -r 's/(^.[^,]*,)([0-9]{2})-([0-9]{2})-([0-9]{4})/\1\4-\3-\2/' file
sed 's/\(^.[^,]*,\)\([0-9][0-9]\)-\([0-9][0-9]\)-\([0-9]\+\)/\1\4-\3-\2/' file

Bash

#!/bin/bash

while IFS="," read -r a b
do
  IFS="-"
  set -- $b
  echo "$a,$3-$2-$1"
done <"file"
眼泪也成诗 2024-09-19 07:30:18

不幸的是,我认为标准 awk 只允许一个字段分隔符,因此您必须预处理数据。您可以使用 tr 来完成此操作,但如果您确实想要一个仅 awk 的解决方案,请使用:

pax> echo '123,01-08-2006
124,01-09-2007
125,01-10-2009
126,01-12-2010' | awk -F, '{print $1"-"$2}' | awk -F- '{print $1","$4"-"$3"-"$2}'

此输出:

123,2006-08-01
124,2007-09-01
125,2009-10-01
126,2010-12-01

根据需要。

第一个 awk, 字符更改为 -,这样您就有四个用相同字符分隔的字段(这是我通常会使用的位)使用tr',''-')。

第二个 awk 按您指定的顺序打印它们,同时更正字段分隔符。

如果您使用的 awk 实现允许多个 FS 字符,则可以使用如下内容:

gawk -F ',|-' '{print $1","$4"-"$3"-"$2}'

Unfortunately, I think standard awk only allows one field separator character so you'll have to pre-process the data. You can do this with tr but if you really want an awk-only solution, use:

pax> echo '123,01-08-2006
124,01-09-2007
125,01-10-2009
126,01-12-2010' | awk -F, '{print $1"-"$2}' | awk -F- '{print $1","$4"-"$3"-"$2}'

This outputs:

123,2006-08-01
124,2007-09-01
125,2009-10-01
126,2010-12-01

as desired.

The first awk changes the , characters to - so that you have four fields separated with the same character (this is the bit I'd usually use tr ',' '-' for).

The second awk prints them out in the order you specified, correcting the field separators at the same time.

If you're using an awk implementation that allows multiple FS characters, you can use something like:

gawk -F ',|-' '{print $1","$4"-"$3"-"$2}'
木落 2024-09-19 07:30:18

如果不需要 awk,你也可以使用 Perl:

$ perl -nle 'print "$1,$4-$3-$2" while (/(\d{3}),(\d{2})-(\d{2})-(\d{4})\s*/g)' < file.txt

If it doesn't need to be awk, you could use Perl too:

$ perl -nle 'print "$1,$4-$3-$2" while (/(\d{3}),(\d{2})-(\d{2})-(\d{4})\s*/g)' < file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文