English 中文(简体)
如何扭转使用AWK的田地的次序?
原标题:How to reverse order of fields using AWK?

我的档案包括:

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
问题回答

Didn t 第一次正确地阅读了这个问题。 你们需要一名能够干.或 com的野外分离者。 如果你能够将干ash用作产出现场分离器(因为它是最常见的),并用分类ake:

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

Pure awk

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

ed

ed -r  s/(^.[^,]*,)([0-9]{2})-([0-9]{2})-([0-9]{4})/14-3-2/  file
ed  s/(^.[^,]*,)([0-9][0-9])-([0-9][0-9])-([0-9]+)/14-3-2/  file

Bash

#!/bin/bash

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

不幸的是,我认为标准<代码>wk只允许一种外地分离特性,因此,必须先处理数据。 您可以通过<代码>tr<>/code>做到这一点,但如果您真的想要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

视需要而定。

第1条<代码>wk 更改<代码>, natures to -,使您拥有四个与同一性质的领域(通常使用<代码>、>)。

第二张<代码>awk按你规定的顺序打印,同时更正外地离职人员。

如果您重新使用<代码>awk执行,允许采用多个<代码>FS。 品格

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

如果不需要k,你也可以使用伯尔语:

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




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签