Operating System - HP-UX
1753862 Members
7405 Online
108809 Solutions
New Discussion юеВ

script help, text file, replace and insert a return

 
SOLVED
Go to solution
patrick xi
Advisor

script help, text file, replace and insert a return

I have a very large text file in only one line. It have fields divided by " ' ".

to simplify it I made an example : abcd'efg'hijkl'mn'opqrs'tuv'

how can I change it to like following:

1) delete " ' " and at the point start another line, output is :
abcd
efg
hijkl
mn
opqrs
tuv

2) start another line after every 3 " ' " charactors, leave " ' " there.

abcd'efg'hijkl'
mn'opqrs'tuv'

I need unix script to do it, thank you for any help.
10 REPLIES 10
steven Burgess_2
Honored Contributor

Re: script help, text file, replace and insert a return

Hi

You can do this with perl. I'm learning this also so have taken the opportunity

For your first method

#-----------------------------------

#!/usr/bin/perl -w

open (MYFILE, "sourcefile") || die "$!";
open (NEWFILE, ">>destination")

while ; {

@newarray=(split (/'/, $_));

foreach $new (0..$#newarray) {
print NEWFILE "newarray[$new]\n";
}
}
close MYFILE;
close NEWFILE;

#--------------------------

Will post second method when I work it out

HTH

Steve
take your time and think things through
curt larson_1
Honored Contributor
Solution

Re: script help, text file, replace and insert a return

1)

cat file | tr "'" "\012"
steven Burgess_2
Honored Contributor

Re: script help, text file, replace and insert a return

heheheh

nice, curt
take your time and think things through
curt larson_1
Honored Contributor

Re: script help, text file, replace and insert a return

2)

cat file | tr "'" "\012" |
awk '{
x++;
if ( x == 3 ) {
printf("$s\047\n",$0);
x=0;
}else{
printf("%s\047",$0);
}}
END {
if ( x < 3 ) printf("\n");
}'
curt larson_1
Honored Contributor

Re: script help, text file, replace and insert a return

1) also could be done

cat file | myPerl.pl
or
myPerl.pl fileName

myPerl.pl contains:
#!/opt/perl/bin/perl -w

while (<>) {
tr/'/\n/;
print;
}
curt larson_1
Honored Contributor

Re: script help, text file, replace and insert a return

and could be done

cat file | myPerl.pl
or
myPerl.pl fileName

myPerl.pl contains:
#!/opt/perl/bin/perl -w

while (<>) {
s/([^']*'[^']*'[^']*')/$1\n/g;
print;
}
patrick xi
Advisor

Re: script help, text file, replace and insert a return

hi, steven and curt, thank you.

but curt,
the output for 2) is not very correct

should be

abcd'efg'hijkl'
mn'opqrs'tuv'

real output is

abcd'efg'$s'
mn'opqrs'$s'

So I think the script should be

2)

cat file | tr "'" "\012" |
awk '{
x++;
if ( x == 3 ) {
printf("%s\047\n",$0); * change $ to % here.
x=0;
}else{
printf("%s\047",$0);
}}
END {
if ( x < 3 ) printf("\n");
}'

thank you, it help me alot!
H.Merijn Brand (procura
Honored Contributor

Re: script help, text file, replace and insert a return

In a script 2) would be easier, because the quotes are ugly in the command prompt

lt09:/tmp 120 > cat xx.txt
abcd'efg'hijkl'mn'opqrs'tuv'
abcd'efg'hijkl'mn'opqrs'
abcd'efg'hijkl'mn'opqrs
abcd'efg'hijkl'mn'
lt09:/tmp 121 > perl -pe's/([^'"']+'"'){3}(?=.)/$&\n/g' xx.txt
abcd'efg'hijkl'
mn'opqrs'tuv'
abcd'efg'hijkl'
mn'opqrs'
abcd'efg'hijkl'
mn'opqrs
abcd'efg'hijkl'
mn'
lt09:/tmp 122 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
patrick xi
Advisor

Re: script help, text file, replace and insert a return

hi procura, quite good one!