Operating System - HP-UX
1828470 Members
3002 Online
109978 Solutions
New Discussion

Re: Script help: divide one file to several and named the new file with pattern in original file

 
SOLVED
Go to solution
patrick xi
Advisor

Script help: divide one file to several and named the new file with pattern in original file

Dear Friends, I need your help.
I have a long one line text file. To simplize it I made a sample fileA, following is its content,
ABC+12345678P1P90+DEF+12345678P2P90+def+12345678P3P90+JKL+12345678P4P90+MNO+12345678P5P90+.......
I want to seperate it to several files using such rule,
1. when match texts in the set {ABC, DEF,def,JKL,MNO}, (case sensitive), create a new file with the match charactors and its following.
2. name the file with 13th-15th charactor(C13,C14,C15) counted from the matching point.
.

the output should like following files,
fileA_P1P:
ABC+12345678P1P90+
fileA_P2P:
DEF+12345678P2P90+
fileA_P3P:
def+12345678P3P90+
fileA_P4P:
JKL+12345678P4P90+
fileA_P5P:
MNO+12345678P5P90+

Hope I've written my mind. Many thanks for any help.

Patrick
6 REPLIES 6
Steve Steel
Honored Contributor

Re: Script help: divide one file to several and named the new file with pattern in original file

Hi

OLDIFS=$IFS
export IFS=+
xx=$(cat $1)
set $(echo $xx)
export IFS=$OLDIFS
set $(echo $1)
let params=${#@}
while [ "$params" -ge "2" ]
do
name=$(echo $1"+"$2"+"|cut -f2 -d"P")
echo $1"+"$2"+" > "fileA_P"$name"P"
shift 2
let params=$params-2
done


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
john korterman
Honored Contributor
Solution

Re: Script help: divide one file to several and named the new file with pattern in original file

Hi,
try the attached script, using your input file as $1

regards,
John K.
it would be nice if you always got a second chance
Rodney Hills
Honored Contributor

Re: Script help: divide one file to several and named the new file with pattern in original file

Pat,

Try this one line perl script...

perl -ne '$x=~s/((ABC|DEF|def|JKL|MNO))/\t$1/g;@a=split("\\t",$x);do { $b=substr($_,12,3); system("echo $_ >fileA_$b") if $b; } for (@a);' theonelinefile

This assumes their are no "tab" characters in the string.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Script help: divide one file to several and named the new file with pattern in original file

Minor change-

perl -ne 's/((ABC|DEF|def|JKL|MNO))/\t$1/g;@a=split("\\t",$_);do { $b=substr($_,12,3); system("echo $_ >fileA_$b") if $b; } for (@a);' theonelinefile

-- Rod Hills
There be dragons...
patrick xi
Advisor

Re: Script help: divide one file to several and named the new file with pattern in original file

Thank you all for the help.

Hi, John, your script is the solution. The only small bug is a file named fileA_ was created besides fileA_P1P to fileA_P5P.

The content of fileA_ is :
MNO++

Best regards,
Patrick
patrick xi
Advisor

Re: Script help: divide one file to several and named the new file with pattern in original file

please close