Force new line after second space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Force new line after second space
# 1  
Old 03-22-2016
Force new line after second space

Hi guys,

I am trying to manipulate a text file.

An example of the file is :

Code:
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:

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

But its doing that on ALL spaces.

How can I force this only on the second space it detects ?
# 2  
Old 03-22-2016
Please, try either of:

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

Code:
perl -nle 'while(/(.*?\s.*?)\s/g){print $1}' longstring.input

This User Gave Thanks to Aia For This Post:
# 3  
Old 03-22-2016
Hello Junaid Subhani,

Could you please try following and let me know if this helps.
Code:
xargs -n2 <  Input_file

Output will be as follows.
Code:
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
These 3 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 03-22-2016
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 ?
# 5  
Old 03-22-2016
Quote:
Originally Posted by Junaid Subhani
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:
Code:
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

Code:
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


Last edited by Aia; 03-22-2016 at 02:16 PM..
# 6  
Old 03-22-2016
try also:
Code:
sed 's/\([^ ]* [^ ]*\) /\1\n/g' infile

or

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

# 7  
Old 03-23-2016
Quote:
Originally Posted by Aia
Please, try either of:

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

Code:
perl -nle 'while(/(.*?\s.*?)\s/g){print $1}' longstring.input

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

Last edited by Scrutinizer; 03-23-2016 at 01:28 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using the entire line with space in between

Hi Folks, I have a report data like the one seen below. FRUITS@NEW_ORANGE(1500 04/29) FRUITS@NEW_ORANGE(1500 05/04) FRUITS@NEW_ORANGE(1500 05/05) FRUITS@NEW_ORANGE(1500 05/07) FRUITS@NEW_ORANGE(1500 05/12) I need to use each of this lines separately in another for loop like the one... (2 Replies)
Discussion started by: jayadanabalan
2 Replies

2. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

3. Shell Programming and Scripting

Check if line has a space

Hi, I want to check if the given line from a text file has a spaces in between. if it does, then I want to add '"' double quotes at the beginning and end of the line. Otherwise leave the line as it is. For example, below is the sample content from my file. $cat file.txt test1 test2... (6 Replies)
Discussion started by: svajhala
6 Replies

4. Shell Programming and Scripting

remove space of each line

Hi Guys, I want remove starting space of each line ... My Input: A B C D E C V F G H F R T Y U D F G H J L O I U Y G P O K O P L O L O I P P O P P P P P My Output: A B C D E C V F G H (7 Replies)
Discussion started by: asavaliya
7 Replies

5. Shell Programming and Scripting

Space at beginning of the line

How can I delete spaces at the begining of all lines of my file ? (2 Replies)
Discussion started by: Sara_84
2 Replies

6. Shell Programming and Scripting

Parse a line by a space

I am trying to show each word on a separate line, but read in all the words on one command line. Below is the code I have right now: read name MyString=$name name2=" " echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \ while read char do if then ... (8 Replies)
Discussion started by: tvb2727
8 Replies

7. Shell Programming and Scripting

Replace end of line with a space

for eg: i have i/p file as: ================ i wnt to change end of line ================= my require ouput is like: i wnt to change end of line ==================== (7 Replies)
Discussion started by: RahulJoshi
7 Replies

8. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

9. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

10. Shell Programming and Scripting

replace space with new line

i have a file named as templist which looks like this: i want to translate spaces to a new line so that the file would look like this im using sed with this sed -e 's/" "/\n/' templist > templist.out but it doesn't work. can someone please help me. (2 Replies)
Discussion started by: dakid
2 Replies
Login or Register to Ask a Question