LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-13-2004, 05:25 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
read the input file and write in the given format


i have input data in a file innthe format
1874500001 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
/CLIP A Y N
1874500002 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
/CLIP A Y N
1874500003 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
1874500004 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
1874500008 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
1874500009 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N
1874500010 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N

i want the data in the format
1874500001 /CWHFE A N N /KWSUBT A Y N /OCBSUB A N N /CLIP
every line starting with ten digit number..
as in second line the data should be pribnted in one complete line..
not able to do..
some lines are in one comltete line... some are not.. how to deal

as i m doing



while read no1 no2
do

if [ ${#no1} -eq 10 ]
then
#echo ${#no1}
echo -n $no1 $no2
else

echo $no1 $no2
fi
done < outty

but those which are already in one complete line.. they start appending
 
Old 12-13-2004, 08:50 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Do you want just append "/CWHFE A N N /KWSUBT A Y N /OCBSUB A N N /CLIP" to the number
found in the beginning of each line ?

Or do you want concatenate /CLIP A Y N to the previous line to make it one line data ?
 
Old 12-13-2004, 11:12 PM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
concatenate /CLIP A Y N to the previous line to make it one line data
 
Old 12-14-2004, 04:03 AM   #4
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
concatenate /CLIP A Y N to the previous line to make it one line data
 
Old 12-14-2004, 05:33 AM   #5
blackzone
Member
 
Registered: Jul 2004
Posts: 256

Rep: Reputation: 30
if it's not a big file. and you don't mind doing it using a stupid way.

just write a c function which read one line at a time and store it in buffer

if it don't start with /CLIP, strcat "\n" and the line

else strcat " " and the line

after you finish save the buffer to file.
 
Old 12-14-2004, 05:36 AM   #6
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
it is a very big file..
this wont be efficient..
anything in shell scripting
 
Old 12-14-2004, 07:12 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
this mess seems to work.
Give it a good test though, especially first and last lines.

Code:
sed -n '

$p
/^[0-9]/ {
            H
         }

/^\/CLIP/ {
           H
           g
           s/^\n//
           s/\n\/CLIP/ \/CLIP/g
           p
           s/.*//
           x
           d
            }
it's in three parts.

$p means print the last line
the H one means if line starts with a digit append it to the 'hold' space (newlines are included)
last long messy bit says,
remove first newline,
remove newlines before '/CLIP'
print it out
delete the line excchange with hold space, delete line (long way of clearing the memoryfor next lines)




regards billy
 
Old 12-14-2004, 10:02 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
Sorry, the last post was wrong!

Not sure how well it will cope with a HUGE file!

(I am at work so can't test fully!!!!)

Code:
cat YOUR_DATA_FILE |
sed -n '

/./ H

/^\/CLIP/ {
           s/.*//
           x
           s/^\n//
           s/\n\/CLIP/ \/CLIP/g
           p
           d
        }

$ {
    x
    s/^\n//
    p
}
for each line:
paste it to the hold memory ##### (the hold memory remembers the lines including the newlines )

Line starts with a '/CLIP' ?
paste it on memory, recall all memory, remove initial '\n' in hold (it adds one before pasting) remove \n before '/CLIP' in all cases
print it.
delete the line (forces read of next line)


last line, if anything in memory
remove the initail '\n' print

else do nothing





billy
 
Old 12-17-2004, 02:12 AM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Here's the Perl soln:
Code:
#!/usr/bin/perl -w
use strict;

my (
    $data_file,
    $data_rec
    );

$data_file = 'test.dat';
open( DATA_FILE, "<$data_file" ) or
                die "Can't open text file: $data_file: $!\n";

while ( defined ( $data_rec = <DATA_FILE> ) )
{
    chomp($data_rec);
    if( $data_rec !~ /^[0-9]/ )
    {
        print "$data_rec";
    }
    else
    {
        print "\n$data_rec";
    }
print "\n";
close (DATA_FILE) or
            die "Can't close input data file: $!\n";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read Write for NTFS file system geeedeee Linux - Laptop and Netbook 8 06-05-2006 08:41 PM
daunting task - read wml input, insert variables into URL, DL page, parse, write file jeffreybluml Programming 1 05-12-2005 07:31 AM
newusers input file format? Pizentios Linux - General 2 04-13-2005 03:27 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 05:36 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 08:23 PM


All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration