niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
oneline file, need newline at every *
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
dwcasey@gmail.com
*nix forums beginner


Joined: 03 Jun 2005
Posts: 22

PostPosted: Wed Jul 19, 2006 3:42 pm    Post subject: oneline file, need newline at every * Reply with quote

I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.

something like

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*

So that when I read in the file, if I hit an asterisk, I create a
newline:

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
Back to top
Glenn Jackman
*nix forums addict


Joined: 19 Apr 2005
Posts: 97

PostPosted: Wed Jul 19, 2006 3:48 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

At 2006-07-19 11:42AM, dwcasey@gmail.com <dwcasey@gmail.com> wrote:
Quote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.


sed -e 's/\*/*\n/g' < myfile > newfile


--
Glenn Jackman
Ulterior Designer
Back to top
dwcasey@gmail.com
*nix forums beginner


Joined: 03 Jun 2005
Posts: 22

PostPosted: Wed Jul 19, 2006 4:00 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

Reply to my own post

tr '*' '\n' < file.txt > file.new


Glenn Jackman wrote:
Quote:
At 2006-07-19 11:42AM, dwcasey@gmail.com <dwcasey@gmail.com> wrote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.


sed -e 's/\*/*\n/g' < myfile > newfile


--
Glenn Jackman
Ulterior Designer
Back to top
Sashi
*nix forums addict


Joined: 14 Jun 2005
Posts: 95

PostPosted: Wed Jul 19, 2006 5:50 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

dwcasey@gmail.com wrote:
Quote:
Reply to my own post

tr '*' '\n' < file.txt > file.new


Glenn Jackman wrote:
At 2006-07-19 11:42AM, dwcasey@gmail.com <dwcasey@gmail.com> wrote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.


sed -e 's/\*/*\n/g' < myfile > newfile


--
Glenn Jackman
Ulterior Designer

That will delete the '*' which is not what the OP wanted.
Back to top
John W. Krahn
*nix forums Guru


Joined: 27 Feb 2005
Posts: 602

PostPosted: Wed Jul 19, 2006 8:57 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

dwcasey@gmail.com wrote:
Quote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.

something like

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*

So that when I read in the file, if I hit an asterisk, I create a
newline:

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*

$ echo "5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*" | \
perl -p052e'$_.="\n"'
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*



John
--
use Perl;
program
fulfillment
Back to top
Chris F.A. Johnson
*nix forums Guru


Joined: 20 Feb 2005
Posts: 2268

PostPosted: Wed Jul 19, 2006 9:27 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

On 2006-07-19, dwcasey@gmail.com wrote:
Quote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.

something like

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*5|12345 North
Street|Smallville|KS|Bob|Smith|5552221212*

So that when I read in the file, if I hit an asterisk, I create a
newline:

5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*
5|12345 North Street|Smallville|KS|Bob|Smith|5552221212*

IF the line is very long, you could have problems using some
utilities. For this, I'd use a filter written in C. This script
writes the C program. compiles it, and runs it (redirect stdin and
stdout as desired):

echo "#include <stdio.h>
int main(void)
{
int c;
while ( (c = getchar()) != EOF ) {
putchar(c);
if ( c == '*' ) putchar('\n');
}
return 0;
}" > xx.c
cc -o xx xx.c
../xx


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Back to top
Geoff Clare
*nix forums addict


Joined: 25 Feb 2005
Posts: 64

PostPosted: Thu Jul 20, 2006 1:19 pm    Post subject: Re: oneline file, need newline at every * Reply with quote

"dwcasey@gmail.com" <dwcasey@gmail.com> wrote, on Wed, 19 Jul 2006:

Quote:
I have a huge, one line file. I need to delimit at every asterisk *
and create a new line.

tr '*' '\n' < file.txt > file.new

The OP wanted to keep the '*'s :

tr '*' '\n' < file.txt | sed 's/$/*/' > file.new

If the input ends with a * this will create an extra empty line
(and then add a '*' to it). To remove the extra '*' line (and any
others with just a '*'):

tr '*' '\n' < file.txt | sed -e 's/$/*/' -e '/^*$/d' > file.new

Removing the extra '*' line without removing any others is left as
an exercise :-)

--
Geoff Clare <netnews@gclare.org.uk>
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Thu Nov 20, 2008 11:47 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Running php file everyday on scheduled time sachin PHP 1 Fri Jul 21, 2006 12:49 pm
No new posts Regarding thesaurus iso file Srikanth modules 0 Fri Jul 21, 2006 10:42 am
No new posts how can i get a file descriptor not used? mars system 0 Fri Jul 21, 2006 7:41 am
No new posts small GTK "Open file" dialog David Siroky Debian 0 Fri Jul 21, 2006 7:30 am
No new posts Trouble Declaring 3D Array in Header File free2klim C++ 1 Fri Jul 21, 2006 4:07 am

Credit Reports | Debt Consolidation | Books | Loans | Debt
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3444s ][ Queries: 16 (0.2545s) ][ GZIP on - Debug on ]