Force new line after second space

Hi guys,

I am trying to manipulate a text file.

An example of the file is :

65450524121|2015-11-20 20:17:59.0 15062464121|2015-12-19 11:44:23.0 01419644121|2016-03-19 09:56:41.0 55146524121|2015-08-17 04:51:55.0 11947314121|2015-12-03 19:25:26.0 29906064121|2015-10-28 18:25:00.0 71585954121|2015-06-10 22:12:13.0 87207383121|2015-11-14 14:15:10.0 77259583121|2015-06-11 16:16:56.0 52186364121|2015-09-25 19:03:33.0 06360373121|2016-02-13 03:01:51.0 42749704121|2015-06-12 11:16:12.0 08136964121|2015-08-02 08:38:16.0 77927982021|2015-06-12 14:03:50.0 29058473121|2016-01-27 08:45:21.0 76950351121|2015-06-12 15:20:28.0 86970264121|2016-03-13 20:01:42.0 99917064121|2016-03-15 08:27:13.0 26313374121|2016-03-05 14:10:40.0 57014064121|2016-03-02 11:22:50.0 23145234121|2015-11-30 17:29:50.0 32740714121|2016-03-02 16:00:43.0 99956193121|2015-11-14 15:56:05.0 68785893121|2015-10-24 19:17:40.0 27418563121|2016-03-13 11:06:56.0 51823193121|2015-07-20 10:49:44.0 75230854121|2016-02-29 13:38:16.0

I want to force a new line after the second space.

I tried to do something like:

cat output.log | tr " " "\n"

But its doing that on ALL spaces.

How can I force this only on the second space it detects ?

Please, try either of:

perl -nle 'while(/(\d{11}\|\d{4}-\d{2}-\d{2} \d+:\d+:\d+.\d)\s/g){print $1}' longstring.input
perl -nle 'while(/(.*?\s.*?)\s/g){print $1}' longstring.input
1 Like

Hello Junaid Subhani,

Could you please try following and let me know if this helps.

xargs -n2 <  Input_file

Output will be as follows.

65450524121|2015-11-20 20:17:59.0
15062464121|2015-12-19 11:44:23.0
01419644121|2016-03-19 09:56:41.0
55146524121|2015-08-17 04:51:55.0
11947314121|2015-12-03 19:25:26.0
29906064121|2015-10-28 18:25:00.0
71585954121|2015-06-10 22:12:13.0
87207383121|2015-11-14 14:15:10.0
77259583121|2015-06-11 16:16:56.0
52186364121|2015-09-25 19:03:33.0
06360373121|2016-02-13 03:01:51.0
42749704121|2015-06-12 11:16:12.0
08136964121|2015-08-02 08:38:16.0
77927982021|2015-06-12 14:03:50.0
29058473121|2016-01-27 08:45:21.0
76950351121|2015-06-12 15:20:28.0
86970264121|2016-03-13 20:01:42.0
99917064121|2016-03-15 08:27:13.0
26313374121|2016-03-05 14:10:40.0
57014064121|2016-03-02 11:22:50.0
23145234121|2015-11-30 17:29:50.0
32740714121|2016-03-02 16:00:43.0
99956193121|2015-11-14 15:56:05.0
68785893121|2015-10-24 19:17:40.0
27418563121|2016-03-13 11:06:56.0
51823193121|2015-07-20 10:49:44.0
75230854121|2016-02-29 13:38:16.0
 

If your requirement is different then the output shown above please do share the more details about it, hope this helps you.

Thanks,
R. Singh

3 Likes

Thank You Ravinder. It was a very simple solution that you provided and it works.

Can you let me know how it works ? man xargs didnt give much.

How does it even detect the spaces ?

xargs builds commands from standard input. The -n will specify maximum number of arguments to use in the execution of the command. The default command is echo.

As a way of observation note: just because a command might fit in a single line or it appears shorter does not mean it is simpler, nor faster, nor better. What's going behind the scenes may be as important as how much you need to enter at the command line.

For instance, look at the execution time of these two commands:

time xargs -n2 < longstring.input
65450524121|2015-11-20 20:17:59.0
15062464121|2015-12-19 11:44:23.0
01419644121|2016-03-19 09:56:41.0
55146524121|2015-08-17 04:51:55.0
11947314121|2015-12-03 19:25:26.0
29906064121|2015-10-28 18:25:00.0
71585954121|2015-06-10 22:12:13.0
87207383121|2015-11-14 14:15:10.0
77259583121|2015-06-11 16:16:56.0
52186364121|2015-09-25 19:03:33.0
06360373121|2016-02-13 03:01:51.0
42749704121|2015-06-12 11:16:12.0
08136964121|2015-08-02 08:38:16.0
77927982021|2015-06-12 14:03:50.0
29058473121|2016-01-27 08:45:21.0
76950351121|2015-06-12 15:20:28.0
86970264121|2016-03-13 20:01:42.0
99917064121|2016-03-15 08:27:13.0
26313374121|2016-03-05 14:10:40.0
57014064121|2016-03-02 11:22:50.0
23145234121|2015-11-30 17:29:50.0
32740714121|2016-03-02 16:00:43.0
99956193121|2015-11-14 15:56:05.0
68785893121|2015-10-24 19:17:40.0
27418563121|2016-03-13 11:06:56.0
51823193121|2015-07-20 10:49:44.0
75230854121|2016-02-29 13:38:16.0

real    0m0.020s
user    0m0.004s
sys     0m0.015s
time perl -nle 'while(/(.*?\s.*?)\s/g){print $1}' longstring.input
65450524121|2015-11-20 20:17:59.0
15062464121|2015-12-19 11:44:23.0
01419644121|2016-03-19 09:56:41.0
55146524121|2015-08-17 04:51:55.0
11947314121|2015-12-03 19:25:26.0
29906064121|2015-10-28 18:25:00.0
71585954121|2015-06-10 22:12:13.0
87207383121|2015-11-14 14:15:10.0
77259583121|2015-06-11 16:16:56.0
52186364121|2015-09-25 19:03:33.0
06360373121|2016-02-13 03:01:51.0
42749704121|2015-06-12 11:16:12.0
08136964121|2015-08-02 08:38:16.0
77927982021|2015-06-12 14:03:50.0
29058473121|2016-01-27 08:45:21.0
76950351121|2015-06-12 15:20:28.0
86970264121|2016-03-13 20:01:42.0
99917064121|2016-03-15 08:27:13.0
26313374121|2016-03-05 14:10:40.0
57014064121|2016-03-02 11:22:50.0
23145234121|2015-11-30 17:29:50.0
32740714121|2016-03-02 16:00:43.0
99956193121|2015-11-14 15:56:05.0
68785893121|2015-10-24 19:17:40.0
27418563121|2016-03-13 11:06:56.0
51823193121|2015-07-20 10:49:44.0

real    0m0.003s
user    0m0.000s
sys     0m0.003s

try also:

sed 's/\([^ ]* [^ ]*\) /\1\n/g' infile

or

awk 'ORS=(NR % 2) ? " " : "\n"' RS=" " infile

Both suggestions will not to print the last pair, because it does not end with whitespace (the newline gets chomped by -l )

Let's fix the bug...

perl -ne 'while(/(\d{11}\|\d{4}-\d{2}-\d{2} \d+:\d+:\d+.\d)\s/g){print "$1\n"}' longstring.input
perl -ne 'while(/(.*?\s.*?)\s/g){print "$1\n"}' longstring.input

...and add one more for this particular case:

perl -pe 's/(\.\d)\s/$1\n/g' longstring.input

The latter does not work (print) with certain awk versions, and it prints a new line at the end.
The following should work with most awk versions

awk '{printf (NR%2 ? "%s " : "%s\n"), $1}' RS=" " infile