Problem in Formatting File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in Formatting File
# 1  
Old 04-11-2006
Problem in Formatting File

I am facing a very challenging task here but can't finish it.I request all of you to help me please.


I have one file which contain some data i need to format it.
data file contain data like

54321|item-68|owner|yes||||$
00-10|invoice|3221|||#
00-11|invoice|3221|||#
00-12|invoice|3221|||#
book1|032|US|yes|||@

54300|item-68|owner|yes||||$
00-211|invoice|3221|||#
00-113|invoice|3221|||#
00-124|invoice|3221|||#
00-135|invoice|3221|||#
bookdoon|032|US|yes|||@
bookdooner|032|US|yes|||@
......................
......................
.....................

Required format:
54321|item-68|owner|yes||||00-10|invoice|3221|||book1|032|US|yes|||
54321|item-68|owner|yes||||00-11|invoice|3221|||
54321|item-68|owner|yes||||00-12|invoice|3221|||
54300|item-68|owner|yes||||00-1|invoice|3221|||bookdoon|032|US|yes|||
54300|item-68|owner|yes||||0-1|invoice|3221|||book3|032|US|yes|||



lines delimited by # may be more than 4 or less and same for line delimited by @.but line prceded by $ will always be one.
to explain more clearly lets take line preceded by $ is A
by # B
by @ D

we have file like
A$
B#
C#
D#
E@


and i need output in a file like
A B E
A C
A D

Last edited by Dhruva; 04-11-2006 at 10:18 AM..
# 2  
Old 04-11-2006
Hope this helps

$ cat tmp1.txt
54321|item-68|owner|yes||||$
00-10|invoice|3221|||#
00-11|invoice|3221|||#
00-12|invoice|3221|||#
book1|032|US|yes|||@

54300|item-68|owner|yes||||$
00-211|invoice|3221|||#
00-113|invoice|3221|||#
00-124|invoice|3221|||#
00-135|invoice|3221|||#
bookdoon|032|US|yes|||@
bookdooner|032|US|yes|||@

$ cat mah.pl
#!/usr/bin/perl

use strict;

open (FH,"<tmp1.txt");

my $line;
my @ar;
my $i;
my $k;
my $fl;

while($line = <FH>)
{
if ( $line =~ /\$$/ ) {

while ( $k > 0 && $k < $i) {
print $fl,$ar[$k++],"\n";
}
chop $line; chop $line;
$fl=$line;
$#ar=-1;
$i=0;
$k=0;
}

if ( $line =~ /\#$/ ) {
chop $line; chop $line;
$ar[$i++]=$line;
}

if ( $line =~ /\@$/ ) {
chop $line; chop $line;
print $fl,$ar[$k++],$line,"\n";
}
}

while ( $k > 0 && $k < $i) {
print $fl,$ar[$k++],$line,"\n";
}

close FH;

$ mah.pl
54321|item-68|owner|yes||||00-10|invoice|3221|||book1|032|US|yes|||
54321|item-68|owner|yes||||00-11|invoice|3221|||
54321|item-68|owner|yes||||00-12|invoice|3221|||
54300|item-68|owner|yes||||00-211|invoice|3221|||bookdoon|032|US|yes|||
54300|item-68|owner|yes||||00-113|invoice|3221|||bookdooner|032|US|yes|||
54300|item-68|owner|yes||||00-124|invoice|3221|||
54300|item-68|owner|yes||||00-135|invoice|3221|||
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting problem

Hello; I have a simple loop filtering a log: for LU in $(< LU-list-Final) do OUT=$(grep -B1 $LU cibc-src-ip.cap |egrep 'IP 16|IP 19|IP 15' |awk -F">" '{print $1}') if ; then echo " LU $LU was accessed by ===============> $OUT " echo "" fi done The current output snippet looks like... (2 Replies)
Discussion started by: delphys
2 Replies

2. UNIX for Advanced & Expert Users

Dos2UNIX formatting problem

Hi, I was trying to dos2unix a file that has some special characters but dos2unix converted those into different format. I am working on sun server. I guess the default for dos2unix on sun server is ISO format . Can i change the format so that it does the conversion in UTF format? Because I... (3 Replies)
Discussion started by: abhi1988sri
3 Replies

3. Shell Programming and Scripting

trite formatting problem

Hello; having an annoying issue: I wish to have the same formatting in: awk '{print $1}' LOCAL f30f31be17a236378ac896639cc1b996 bff4c460f601444db6ef7f6ad6ca44b9 347a399b6fe9c2f21e6a7f55911c1483 ce3f8fdd4919e891090ca27872f4f983 c00098663f064d14065d0ef248a4db44... (2 Replies)
Discussion started by: delphys
2 Replies

4. Shell Programming and Scripting

Problem in formatting number

Hi, I was trying to format my number like i=1 to 000001 using the below method. typeset -Z6 i (sorry, corrected) My shell is K, is not doing, it is supposed to do Thanks in advance (6 Replies)
Discussion started by: ezee
6 Replies

5. Shell Programming and Scripting

Problem with formatting text with awk

I want the following output: User ID: 4071 Last Name: Gills First Name: Roberts Address: Maple Dr. Phone#: 702346789 from this command: grep "$uId" database.txt | awk -F":" '{print "User ID:\t"$uId"\nLast Name:\t"$lname"\n...etc. }' But all I get is this: User ID:... (3 Replies)
Discussion started by: yonkers062986
3 Replies

6. UNIX for Dummies Questions & Answers

Conversion problem with date field and formatting.

Hi, My input file contains the record(s) as below with space as FS. 01-01024180 35000 MV010 02/03/09 0306 03060226 03 02-00410330 470000 MV010 02/03/09 0301 03010276 03 1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator. Using the modified... (2 Replies)
Discussion started by: talk2pawee
2 Replies

7. Shell Programming and Scripting

output formatting problem

I would like to keep the complete lines in the output, but my script adds carriage returns for each space (e.g. keep BRITISH AIRWAYS on one line in the output): File1= BAW BRITISH AIRWAYS RYR RYAN AIR for i in $(cat File1) do echo $i done Output: BAW BRITISH AIRWAYS RYR... (4 Replies)
Discussion started by: barny
4 Replies

8. Shell Programming and Scripting

AWK Formatting Problem

Hi All, I'm having a problem with the way awk is interperting a space between double quotes in a for loop. Below is the code and output from running the script: AWK for loop: for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{ print... (2 Replies)
Discussion started by: cstovall
2 Replies

9. Shell Programming and Scripting

Formatting Problem

Hi Suppose we have a file consisting of nos in following format 123 - 789 123 - 828 345 - 989 345 - 792 I require the following output 123, 789,828 345, 989,792 Means Unique nos in 1st Column and Corresponding two nos in comma separated 2nd Column Please help me out... (6 Replies)
Discussion started by: PradeepRed
6 Replies

10. UNIX for Dummies Questions & Answers

Problem re-formatting Disk Partition

I have a disk formatted as follows. Part Flag Tag Cylinders Size =================================================== 0 wm root 0 - 38125 26.18 Gb 1 wu swap 38126 - 49776 8 Gb 2 wm backup 0 - 49779 34.18 Gb 3 wm unassigned 4 wm unassigned 5 wm unassigned 6 wm unassigned... (1 Reply)
Discussion started by: jimthompson
1 Replies
Login or Register to Ask a Question