Unix Shell - how to make a file from the strings in a file and redirect the content to these new fi

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > how to make a file from the strings in a file and redirect the content to these new fi





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to make a file from the strings in a file and redirect the content to these new fi
rash

2004-10-07, 5:49 pm

Here is scenari0
223 files copied to this this one big flat file.

now how to make this file to 223 files.

further explaination:
below is excerpt from that one file under xxxxx
you will see
@TC_ID: :BIC4007
then another @TC_ID: :BIC40008 and
has a 223 time with TC_ID: :BICxxx numbers

How do I use such a script that makes a file named BIC40008, then
another BIC40007
AND copying the content from line @TC_ID: :BIC40007 till one line
before the next TC_ID: :BIC40008



xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
@TC_ID: :BIC40007
@TC_TITLE:
@ACT_IDS:
@AUTHOR:
@AUTO:
@CLASS:
@GROUP_ID:
@RELEASES:
@TC_DETAILS_FILENAME:
@BILLING_FILENAME:

@TC_ID: :BIC40008
@TC_TITLE:
@ACT_IDS:
@AUTHOR:
@AUTO:
@CLASS:
@GROUP_ID:
@RELEASES:
@TC_DETAILS_FILENAME:
@BILLING_FILENAME:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx

I have tried using this script only creating these files
but doesnt go that far
>grep -i TC_ID testcases | cut -c 8-16 | xargs -i touch '{}' .


but just makes only 146 files rather then 223 files

any ideas

thanks
in advance
John L

2004-10-07, 5:49 pm


"rash" <rashidvoip@yahoo.com> wrote in message news:5bd5ead2.0410071050.2feba65d@posting.google.com...
> Here is scenari0
> 223 files copied to this this one big flat file.
>
> now how to make this file to 223 files.
>
> further explaination:
> below is excerpt from that one file under xxxxx
> you will see
> @TC_ID: :BIC4007
> then another @TC_ID: :BIC40008 and
> has a 223 time with TC_ID: :BICxxx numbers
>
> How do I use such a script that makes a file named BIC40008, then
> another BIC40007
> AND copying the content from line @TC_ID: :BIC40007 till one line
> before the next TC_ID: :BIC40008
>
>
>
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> @TC_ID: :BIC40007
> @TC_TITLE:
> @ACT_IDS:
> @AUTHOR:
> @AUTO:
> @CLASS:
> @GROUP_ID:
> @RELEASES:
> @TC_DETAILS_FILENAME:
> @BILLING_FILENAME:
>
> @TC_ID: :BIC40008
> @TC_TITLE:
> @ACT_IDS:
> @AUTHOR:
> @AUTO:
> @CLASS:
> @GROUP_ID:
> @RELEASES:
> @TC_DETAILS_FILENAME:
> @BILLING_FILENAME:
>
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx
>
> I have tried using this script only creating these files
> but doesnt go that far
>
> but just makes only 146 files rather then 223 files
>
> any ideas
>


Probably best to use awk or perl, both of which have their
own newsgroups.

Here is an awk script to do what you want:

BEGIN { FS = ":" }
$1 == "@TC_ID" { file = $3 }
NF > 0 { print >> file }
NF == 0 { close(file) }

Save it in a file called (say) filesplitter and use it as:
awk -f filesplitter your_one_big_flat_file

The first line sets the field separator (FS) to colon (.
The second line sets file to be the third field ($3) of
any line whose first field ($1) is "@TC_ID". (Note == not = .)
The third line prints any line with more than one field to file.
The fourth line closes file on encountering a blank line.

HTH.

--
John.


Robert Bonomi

2004-10-07, 5:49 pm

In article <5bd5ead2.0410071050.2feba65d@posting.google.com>,
rash <rashidvoip@yahoo.com> wrote:
>Here is scenari0
>223 files copied to this this one big flat file.
>
>now how to make this file to 223 files.
>
>further explaination:
>below is excerpt from that one file under xxxxx
>you will see
>@TC_ID: :BIC4007
>then another @TC_ID: :BIC40008 and
>has a 223 time with TC_ID: :BICxxx numbers
>
>How do I use such a script that makes a file named BIC40008, then
>another BIC40007
>AND copying the content from line @TC_ID: :BIC40007 till one line
>before the next TC_ID: :BIC40008


*IF* the 'sections' are all exactly the same number of lines, then
'man split'.

Otherwise, this is an 'awkward' job, thus _awk_ is right tool.

1) when the string ^@TC_ID: is matched, extract the 2nd 'word' on the
line, and save it for use as the output file name.
2) for each line, print to a file named with the saved value.

housekeeping, when you change the output file name, you will probably
need to close previously open output file, to avoid exhausting file handles.

>
>
>
>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>@TC_ID: :BIC40007
>@TC_TITLE:
>@ACT_IDS:
>@AUTHOR:
>@AUTO:
>@CLASS:
>@GROUP_ID:
>@RELEASES:
>@TC_DETAILS_FILENAME:
>@BILLING_FILENAME:
>
>@TC_ID: :BIC40008
>@TC_TITLE:
>@ACT_IDS:
>@AUTHOR:
>@AUTO:
>@CLASS:
>@GROUP_ID:
>@RELEASES:
>@TC_DETAILS_FILENAME:
>@BILLING_FILENAME:
>
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx
>
>I have tried using this script only creating these files
>but doesnt go that far
>
>but just makes only 146 files rather then 223 files
>
>any ideas
>
>thanks
>in advance



rakesh sharma

2004-10-08, 2:47 am

rashidvoip@yahoo.com (rash) wrote in message news:

>
> Here is scenari0
> 223 files copied to this this one big flat file.
>
> now how to make this file to 223 files.
>
> further explaination:
> below is excerpt from that one file under xxxxx
> you will see
> @TC_ID: :BIC4007
> then another @TC_ID: :BIC40008 and
> has a 223 time with TC_ID: :BICxxx numbers
>
> How do I use such a script that makes a file named BIC40008, then
> another BIC40007
> AND copying the content from line @TC_ID: :BIC40007 till one line
> before the next TC_ID: :BIC40008
>


The right tool for this scenario is 'csplit' aka "context split"
and preferably from the GNU stable:

csplit -f BIC40 your_large_inp_file '/@TC_ID: :/' '{*}'
rash

2004-10-08, 5:52 pm

gawk -vRS="@TC_ID: :" 'NR>1{printf "%s%s",RS,$0 > $1}' input


This above command worked
EXCELLENT
and csplit is little complicated for me

thank you guys

Rash

Ed Morton <morton@lsupcaemnt.com> wrote in message news:<ck4hdr$2t7@netnews.proxy.lucent.com>...
> rash wrote:
>
> This should do it:
>
> gawk -vRS="@TC_ID: :" 'NR>1{printf "%s%s",RS,$0 > $1}' input
>
> Regards,
>
> Ed.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com