| Author |
Message |
Geoff Clare *nix forums addict
Joined: 25 Feb 2005
Posts: 64
|
Posted: Thu Jul 20, 2006 1:19 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
"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 |
|
 |
Chris F.A. Johnson *nix forums Guru
Joined: 20 Feb 2005
Posts: 2268
|
Posted: Wed Jul 19, 2006 9:27 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
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 |
|
 |
John W. Krahn *nix forums Guru
Joined: 27 Feb 2005
Posts: 602
|
Posted: Wed Jul 19, 2006 8:57 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
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 |
|
 |
Sashi *nix forums addict
Joined: 14 Jun 2005
Posts: 95
|
Posted: Wed Jul 19, 2006 5:50 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
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 |
|
 |
dwcasey@gmail.com *nix forums beginner
Joined: 03 Jun 2005
Posts: 22
|
Posted: Wed Jul 19, 2006 4:00 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
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 |
|
 |
Glenn Jackman *nix forums addict
Joined: 19 Apr 2005
Posts: 97
|
Posted: Wed Jul 19, 2006 3:48 pm Post subject:
Re: oneline file, need newline at every *
|
|
|
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
|
Posted: Wed Jul 19, 2006 3:42 pm Post subject:
oneline file, need newline at every *
|
|
|
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 |
|
 |
Google
|
|
| Back to top |
|
 |
|