Operating System - HP-UX
1832617 Members
2628 Online
110043 Solutions
New Discussion

How to insert a New Line for every occurance of the ' char?

 
SOLVED
Go to solution
Declan Mc Kay
Occasional Advisor

How to insert a New Line for every occurance of the ' char?

Hi, I need to run a cmd which will format a file by inserting a New line after each occurance of the ' character,,,,I know this is probably easily done via awk/sed but I can't figure how. Eg as follows:

UNB+UNOA:2+AISUPP+AIHUB+040107:1300+AISPAIHB009329++INVOIC'UNG+INVOIC+AISUPP+AIHUB+040107:1300+877+UN+D:96A:EAN008'UNH+1+INVOIC:D:96A:UN:EAN008'

I need the above as follows:

UNB+UNOA:2+AISUPP+AIHUB+040107:1300+AISPAIHB009329++INVOIC'
UNG+INVOIC+AISUPP+AIHUB+040107:1300+877+UN+D:96A:EAN008'
UNH+1+INVOIC:D:96A:UN:EAN008'

Anyone any ideas as to how this can be done..?
10 REPLIES 10
Umapathy S
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

I can give a technique in vi which can be used with sed also.

open the file in vi
:%s:':'^M:g

where the chars between 3 and 4th colons are
'+V +

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Jdamian
Respected Contributor

Re: How to insert a New Line for every occurance of the ' char?

Try

cat file | awk '{ gsub("'"'"'","'"'"'\n"); print }'
H.Merijn Brand (procura
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

# perl -pe's/\x27/\n/g' your_file

and if you want to change the file

# perl -pi -e's/\x27/\n/g' your_file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Bill Hassell
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

Really easy in tr:

tr -s "'" "[\012*]" file_name


Bill Hassell, sysadmin
Mark Grant
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

Bearing in mind that you appear to need the "'" character to remain, you might need to adjust all of the above solutions slightly. I think Bill's "tr" option won't actually work now :(
Never preceed any demonstration with anything more predictive than "watch this"
Michael Schulte zur Sur
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

Hi,

my 2 cents:

BEGIN{}
{
for (i = 1; i < NF; i++) printf("%s'\n", $i);
printf("%s\n", $NF);
}
END{}

call awk -F\' file

greetings,

Michael
Graham Cameron_1
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

Not as elegant as the earlier awk script, but works...

awk -F"'" '{for (i=1;i
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Declan Mc Kay
Occasional Advisor

Re: How to insert a New Line for every occurance of the ' char?

Thanks for the answers.
Procura your perl script seems to work best but doesn't keep the ' which isn't a big deal. The other e.g's which used awk seemed to fail because of the following:
awk: Input line UNB+UNOA:2+AISUPP+AI cannot be longer than 3,000 bytes.
The source line number is 1.

If you have an idea to keep the ' that would be brilliant. Tks.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: How to insert a New Line for every occurance of the ' char?

# perl -pe's/\x27/\x27\n/g' your_file

I use \x27 as a way to write a single quote in a shell one-liner.

if this is inside a script, you can just use

--8<---
#!/opt/perl/bin/perl

use strict;
use warnings;

while (<>) {
s/'/'\n/g;
print;
}
-->8---
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Michael Schulte zur Sur
Honored Contributor

Re: How to insert a New Line for every occurance of the ' char?

Hi,

this may also work:
/'/{
s/'/'\
/g
}

put it in a file and call with:
sed -f yourfile.sed yourfile

have fun,

Michael