Operating System - HP-UX
1823228 Members
3595 Online
109648 Solutions
New Discussion юеВ

Cheap points for script gurus !! ;p

 
SOLVED
Go to solution
kent friberg
New Member

Cheap points for script gurus !! ;p

Hey gurus,

I have a file that I know will contain these values at a FIXED position:

T200
R200

I want to split this one file into two separate files. One file containing the T200's and one file containing the R200's.

Now, this would be a piece of cake had it not been for the fact that T200 could also appear on "R200-lines" and vice versa. The only thing that will help me is the fact that the T200's and R200's do infact come atleast once and in the exact same spot each time.

Examples:

FileA without problem:
sometextT200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo
sometextT200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo

FileB with problem:
sometextT200sometextheretoo
sometextR200sometextherT200
sometextR200sometextheretoo
sometextT200sometextherR200
sometextR200sometextheretoo
sometextR200sometextheretoo

see my problem ?

Please help !!??
15 REPLIES 15
Patrick Wallek
Honored Contributor

Re: Cheap points for script gurus !! ;p

Can you show us an example of the output you expect in both cases? I am confused about the format of the output file that you want.
A. Clay Stephenson
Acclaimed Contributor

Re: Cheap points for script gurus !! ;p

Hi Kent:

Attached is a shell script which calls awk. You will need to adjust the shell variable "OFFSET" to match the fixed location of your data.

Clay

If it ain't broke, I can fix that.
kent friberg
New Member

Re: Cheap points for script gurus !! ;p

ahhh.yes..

OriginalFileA without problem:
sometextT200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo
sometextT200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo

I want to be split into two files like this:

File1
sometextT200sometextheretoo
sometextT200sometextheretoo

File2
sometextR200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo

(and same with)
OriginalFileB with problem:
sometextT200sometextheretoo
sometextR200sometextherT200
sometextR200sometextheretoo
sometextT200sometextherR200
sometextR200sometextheretoo
sometextR200sometextheretoo

I want split into:

File1:
sometextT200sometextheretoo
sometextT200sometextherR200

File2:
sometextR200sometextherT200
sometextR200sometextheretoo
sometextR200sometextheretoo
sometextR200sometextheretoo

Hope this was not too fuzzy .. ?:-\
A. Clay Stephenson
Acclaimed Contributor

Re: Cheap points for script gurus !! ;p

Hi Kent:
Oops wrong one, this should do it.

Attached is a shell script which calls awk. You will need to adjust the shell variable "OFFSET" to match the fixed location of your data.


Clay
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Cheap points for script gurus !! ;p

OK Kent,

I am not a guru but will try to help. This is really dependent on what your fixed position is. If you can show us the exact files, it may be easy to figure out.

If we just take the example you gave, i would do this way.

For ex., if you have the text like this
sometextT200sometextR200
sometextT200sometextT200
sometextR200sometextT200

If I want to get all T200 lines I would do

grep "^sometextT200" file > t200.stuff


If there is anything that is before the fixed position, then you can use that string in your grep command.

If it is a fixed field, you can use awk to match that field and print the line.

It's just dependent on what you have in the file.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Patrick Wallek
Honored Contributor
Solution

Re: Cheap points for script gurus !! ;p

This may be simplistic, but what about grep?

grep T200 file1 >> T200.out
grep R200 file1 >> R200.out

That will pick up all occurrences of T200 and R200 in the file.
Santosh Nair_1
Honored Contributor

Re: Cheap points for script gurus !! ;p

This may seem cheesy but how about something like this

grep "^........R200.*" testfile

where you pad out the dots till the first occurance of R200 (the dot, '.', represents any character). You could do the same for T200. Not the most elegant solution, but it works.

-Santosh
Life is what's happening while you're busy making other plans
Alan Riggs
Honored Contributor

Re: Cheap points for script gurus !! ;p

Clay's solution should work, though I personally would have included the output to each file within the awk program. Under the header of "there's always another way", how about using basic shell commands.

while read LINE
do
KEY=$(echo $LINE |cut -c 0-12)
case $KEY in
T200)
echo $LINE >> file1
;;
R200)
echo $LINE >> file2
*)
;;
esac
done < infile


If you know that every line MUST have either R200 or T200 in the key position, drop the case and just use an if..then..else
Duane Gorder
Advisor

Re: Cheap points for script gurus !! ;p

You already have lots of answers, but here is another one attached to this reply.
Live each season as it passes; breathe the air,
linuxfan
Honored Contributor

Re: Cheap points for script gurus !! ;p

Hi Kent,

I was about to send in the reply when i saw Alan's reply which was almost what i was submitting anyway.

I only had a small modification to that , since you already know the exact location, you want to use
cut -c 9-12 for the example you gave.
where 9 is the beginning and 12 is the end. (man cut for more information)


-Ramesh
They think they know but don't. At least I know I don't know - Socrates
Satish Y
Trusted Contributor

Re: Cheap points for script gurus !! ;p

Hi Kent,

I agree with both Patrick and Sridhar's solutions, they r simple and good. I don't think u need to go for spl scripts for that.

Cheers...
Satish.
Difference between good and the best is only a little effort
Wodisch
Honored Contributor

Re: Cheap points for script gurus !! ;p

Hello Kent,

Santosh's answer is almost perfect, the only
improvement I can see is the omit ".*" at the
end of his pattern - it is not needed there.

Just my ?0.02,
Wodisch (I am european, but that should be about $0.02)

Stefan Schulz
Honored Contributor

Re: Cheap points for script gurus !! ;p

Hi Kent,

i would use no awk or perl, just a little bit grep:

grep "^........T200" >> t200.txt
grep "^........R200" >> r200.txt

Put in as many dots as you need as a placeholder for your fixed length Text befor your T200/R200 string. This grep says: grep every line starting with 8 characters of any type and the string T200. And don't care whats behind.

This is not a impressing solution, but it is working and really fast.

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Andreas Voss
Honored Contributor

Re: Cheap points for script gurus !! ;p

Hi,

here my solution:

#!/bin/sh

:>File1
:>File2

awk '{
t=index($0, "T200")
r=index($0, "R200")
if(t>0)
{
if((r>0 && t print $0 >>"File1";
else
print $0 >>"File2";
}
else if(r>0)
{
if((t>0 && r print $0 >>"File2";
else
print $0 >>"File1";

}
}' File

Regards
kent friberg
New Member

Re: Cheap points for script gurus !! ;p

Huge thanks guys,

I hope I've given everyone points now.

ohh..btw:

grep "^........T200"

did the trick...

..there's som much to learn..

;)