简体   繁体   中英

Print matched field separators in awk

Given the following record

01-01-2012 18:02 some data 01-11-2014 20:22 some other data 10-02-2014 14:00 more data still

I am trying to group date, time and data and print them on separate lines like so:

01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still

However, what I have so far:

echo '01-01-2012 18:02 some data 01-11-2014 20:22 some other data 10-02-2014 14:00 more data still' | awk -F '[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*' '{ for ( n=1; n<=NF; n++ ) print $n }

results in this:

 some data 
 some other data 
 more data still

The dates and times are missing. They are field separators, therefore they don't print.

How can I modify my awk script in order to print every field separator that matches the regex?

Using gnu awk:

awk -v RS='[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+' '!NF{s=RT;next} {print s $0}' file
01-01-2012 18:02 some data
01-01-2012 18:02 some other data
01-01-2012 18:02 more data still

EDIT: Using non-gnu awk you can do:

awk '{gsub(/[[:blank:]]+[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+/, "\n&"); 
      gsub(/\n[[:blank:]]+/, "\n")} 1' file
01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still

Also using grep -P you can do:

grep -oP '[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+.+?(?=[0-9]+-[0-9]+-[0-9]+|$)' file
01-01-2012 18:02 some data
01-11-2014 20:22 some other data
10-02-2014 14:00 more data still

awk方式

awk '{for(i=2;i<=NF;i++)if($i~/[0-9]+-[0-9]+-[0-9]+/)$i="\n"$i}1' file

by awk

awk '{for (i=1;i<=NF;i++) printf ($i~/-..-/)?RS $i:FS $i}' infile

explanation

  • for loop : read the element one by one, elements are splitted by whitespace.
  • printf : print the element without return
  • printf ($i~/-..-/)?RS $i:FS $i - can be expended to if-else statement: if ($i~/-..-/) {print RS $i) else (print FS $i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM