LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2018, 12:08 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
split line after first space


This one i will never get there in this life to figure it out , but dor some here it could be a piece of cake because they problably do scrits all day .

Here it is the thing .

by default , to split a text i use tr command like this :
Quote:
tr " " "\n"<infile >outfile
but this command will not work if i have a file like this :
Quote:
http://balaldhfhkffefef my data something http://ghsdghsdk studd orogg
what i need in this case on the output , is something like this :

Quote:
http://balaldhfhkffefef
my data something
http://ghsdghsdk
studd orogg
etc....
I need tr or any other tool to look into the line , split after first space to a new line , and then look for the splited line , get the other url in another line and etc ....

Is this even possible ?
i know it is , but it will probably a command with 500 letters .
 
Old 02-02-2018, 12:53 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
With a standard sed the following should work
Code:
sed '
  s#$# #
  s#\(https\{0,1\}://[^[:space:]]*\)[[:space:]]#\1\
#g
  s# $##
' < infile > outfile
Note that a standard sed uses a backslash followed by a newline in order to print a newline. GNU sed also takes \n for that.
--
Hmm, something is still missing...

Last edited by MadeInGermany; 02-02-2018 at 01:00 PM.
 
Old 02-02-2018, 01:06 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thanks for the output on this one .
It only works for the 1st line .

the output generated on that command using the text i provided is :
Quote:
http://balaldhfhkffefef
my data something http://ghsdghsdk
studd orogg
 
Old 02-02-2018, 02:29 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Using 2 passes:
Code:
sed -re 's| ?(https?://[^ ]*) |\n\1\n|g' -e 's/^\n//' infile > outfile

Last edited by keefaz; 02-02-2018 at 02:33 PM.
 
1 members found this post helpful.
Old 02-02-2018, 04:30 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Worked like a charm , i was hopping to be much more complicated this process .
Nice work and thank you for your contribution .
 
Old 02-04-2018, 11:35 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
For symmetry reason, and indeed covering a line that ends with a URL:
Code:
sed -r 's| ?(https?://[^ ]*) ?|\n\1\n|g; s/^\n//; s/\n$//'

Last edited by MadeInGermany; 02-07-2018 at 03:12 AM.
 
Old 02-04-2018, 11:47 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
For sake of variety, this is an awk solution.

With this InFile ...
Code:
http://fruit apple banana cherry http://colors red blue  
http://cars honda toyota http://flavors chocolate vanilla 
http://seasons summer fall winter spring
... this awk ...
Code:
awk 'BEGIN{RS="http"} 
  {if (NR>1) {print RS$1; $1="";
              print substr($0,2)}}'  \
$InFile >$OutFile
... produced this OutFile ...
Code:
http://fruit
apple banana cherry
http://colors
red blue
http://cars
honda toyota
http://flavors
chocolate vanilla
http://seasons
summer fall winter spring
Daniel B. Martin

.
 
Old 02-05-2018, 09:14 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Another sed solution ...

With this InFile ...
Code:
http://fruit apple banana cherry http://colors red blue  
http://cars honda toyota http://flavors chocolate vanilla 
http://seasons summer fall winter spring
... this code produced ...
Code:
sed 's/ http/\nhttp/g' $InFile |sed 's/ /\n/' >$OutFile
... produced this OutFile ...
Code:
http://fruit
apple banana cherry
http://colors
red blue
http://cars
honda toyota
http://flavors
chocolate vanilla
http://seasons
summer fall winter spring
Daniel B. Martin

.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I use the command line to split a single-lined XML into a multi-line XML xexers Linux - Software 3 12-09-2010 07:25 AM
[SOLVED] bash: Split a text file into an array? (NOT line-by-line) DJCharlie Programming 9 09-19-2010 09:22 PM
[bash] Read file line by line and split on whitespace tskuzzy Programming 4 07-06-2009 03:24 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration