Reputation: 355
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?
Upvotes: 1
Views: 342
Reputation: 45353
by awk
awk '{for (i=1;i<=NF;i++) printf ($i~/-..-/)?RS $i:FS $i}' infile
for loop
: read the element one by one, elements are splitted by whitespace.printf
: print the element without returnprintf ($i~/-..-/)?RS $i:FS $i
- can be expended to if-else statement:
if ($i~/-..-/) {print RS $i) else (print FS $i)
Upvotes: 1
Reputation:
awk way
awk '{for(i=2;i<=NF;i++)if($i~/[0-9]+-[0-9]+-[0-9]+/)$i="\n"$i}1' file
Upvotes: 1
Reputation: 786359
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
Upvotes: 1