- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: scripting question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 08:35 AM
тАО05-19-2004 08:35 AM
Here's what I need help with:
I receive a data file with several records inside and each record has several lines.
The file follows the following format:
H0001
D0001
D0001
D0001
T0001
H0002
D0002
D0002
T0002
H0003
...., etc.
The H indicates a Header Record, the D a Detail Record and T a Terminator Record.
The H,D or T is actually the first character of the line and the number of Detail lines varies.
What I need to do is split the single file into multiple files so that each H-D-D-D-T section of the file is in a separate file with a different name. The file name is doesn't matter much, perhaps a file.timestamp format. I'm not getting very far with it and was wondering if I could get some help or direction.
I've got perl on the system but I'm not very conversant with it, a shell script would be prefered for longer term support. Or, of course, I could start learning perl ;-)
Thanks a Lot
Gary
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 08:45 AM
тАО05-19-2004 08:45 AM
Re: scripting question
file="f"
num=0
cat yourFile |
while read var
do
case $var in
H*) ((num = $num + 1))
print "$var" > ${file}${num}
;;
D*|H*) print "$var" >> ${file}${num}
;;
esac
done
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 08:48 AM
тАО05-19-2004 08:48 AM
Re: scripting question
Here's something off the top of my head ---
#!/usr/bin/sh
while read LINE
do
FIRST=$(echo $LINE | cut -c 1)
if [ "${FIRST}" = "H" ] ; then
FILE=H_$(date +%m%d%Y)_$(date +%H%M%S)
echo ${LINE} >> ${FILE}
else
echo ${LINE} >> ${FILE}
fi
This should give you a file named like H_05192004_154715 starting with an H line, it will write to that file until it finds another record with H as the first character then it will start a new file. I haven't tested, but I think it should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 08:55 AM
тАО05-19-2004 08:55 AM
Re: scripting question
cat yourFile | awk '
BEGIN {name="f";num=0;}
/^H/ {
num += 1;
fname=sprintf("%s%d",name,num);
print $0 > fname;
next;
}
/^D/ {
print $0 >> fname
next;
}
/^T/ {
print $0 >> fname
}
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 08:58 AM
тАО05-19-2004 08:58 AM
Re: scripting question
Here is one way to do it in Perl:
#!/usr/bin/perl
while (<>)
{
if (/^H/){
close (OUTF);
$count++;
$outfile="FILE." . $count;
open(OUTF,">$outfile") or die "Can't open output file $outfile";
}
print OUTF $_;
}
JP
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 09:13 AM
тАО05-19-2004 09:13 AM
Re: scripting question
We do just this thing on a similar data file.
Attached is a snip of the process with our naming convention edited.
The array allows the cylcle through and sed of the lines you want printed to separate files.
Not this snip is based on each new record starting with H, and H does not appear in the data portion.
Best of luck.
Regards,
dl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 09:17 AM
тАО05-19-2004 09:17 AM
Re: scripting question
This will create files named 1,2,3 etc. The numbers will correspond to the end of the H.
One of the other ways might be better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 09:28 AM
тАО05-19-2004 09:28 AM
Re: scripting question
My two lines awk script:
awk '
/^H/ {count++ ; filename="prefix_" count}
{ print >> filename }' filein
where "prefix_" is a prefix you want to name the out files.
Frank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 09:43 AM
тАО05-19-2004 09:43 AM
Solution#!/usr/bin/sh
COUNT=0
while read LINE
do
FIRST=$(echo $LINE | cut -c 1)
echo $FIRST ; if [ "${FIRST}" = "H" ] ; then
FILE=H_${COUNT}_$(date +%m%d%Y)_$(date +%H%M%S)
echo ${LINE} >> ${FILE}
let COUNT=$COUNT+1
else
echo ${LINE} >> ${FILE}
fi
done < datfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-19-2004 01:38 PM
тАО05-19-2004 01:38 PM
Re: scripting question
There must be an easy solution with csplit. I try from memory, so please don't hit me. ;-).
hth,
Michael
csplit -f spl filetosplit /^H[0-9]*/