Operating System - HP-UX
1833747 Members
2839 Online
110063 Solutions
New Discussion

Re: Help with script to edit a text file

 
SOLVED
Go to solution
Robert Krebs
Occasional Advisor

Help with script to edit a text file

I have an application that generates a text file in a format similar to the following:

DISK$MMCP1:[CXMMCP.V0200_123]FILE1.TLF -
/DIRECTORY_NAME=SYSWORK1 /FILE_NAME=FILE1 /CLASSIFICATION=U -
/READ /FILTYPE=1 /FILESIZE=1657 /RECTYPE=N /RECSIZE=512 /RECCOUNT=1657
DISK$MMCP3:[CXMMCP.V0100_123]FILE2.TLF -
/DIRECTORY_NAME=SYSWORK2 /FILE_NAME=FILE2 /CLASSIFICATION=U -
/READ /FILTYPE=1 /FILESIZE=1657 /RECTYPE=N /RECSIZE=512 /RECCOUNT=1657
DISK$MMCP1:[CXMMCP.ABRACADAB]FILE3.TLF -
/DIRECTORY_NAME=SYSWORK3 /FILE_NAME=FILE3 /CLASSIFICATION=U -
/READ /FILTYPE=3 /FILESIZE=29 /RECTYPE=N /RECSIZE=512 /RECCOUNT=29


This file is used by another application and needs to be edited to make it look like this:

FILE1.TLF /DIRECTORY_NAME=SYSWORK1 /FILE_NAME=FILE1 /CLASSIFICATION=U /READ /FILTYPE=1 /FILESIZE=1657 /RECTYPE=N /RECSIZE=512 /RECCOUNT=1657
FILE2.TLF /DIRECTORY_NAME=SYSWORK2 /FILE_NAME=FILE2 /CLASSIFICATION=U /READ /FILTYPE=1 /FILESIZE=1657 /RECTYPE=N /RECSIZE=512 /RECCOUNT=1657
FILE3.TLF /DIRECTORY_NAME=SYSWORK3 /FILE_NAME=FILE3 /CLASSIFICATION=U /READ /FILTYPE=3 /FILESIZE=29 /RECTYPE=N /RECSIZE=512 /RECCOUNT=29

(In case the formatting is jumbled in the ITRC - the original file has three lines/record, each ending with a "-" and linefeed character. The desired file needs to have information deleted from the beginning of each record and should have one line/record with no "-".

The file sometimes has upwards of 100 records. Manual editing with vi is tedious and I've tried to come up with a script to use sed, awk or perl to complete the editing but have had little success. I usually run headlong into a brick wall when replacing the linefeed characters at the end the first and second lines.

Your assistance is greatly appreciated!
Have a grand day, Bob
Admission of wrongdoing is not an admission of weakness, but a sign of strength - Author Unknown
8 REPLIES 8
John Palmer
Honored Contributor

Re: Help with script to edit a text file

This simple shell script will do what you want...

#!/usr/bin/sh
while read A B C D E
do
case ${A} in
DISK*) FNAM=${A#*\]}
print -n "$FNAM" ;;
/DIR*) print -n "${A} ${B} ${C}" ;;
/READ*) print "${A} ${B} ${C} ${D} ${E}" ;;
esac
done

Regards,
John
Ramkumar Devanathan
Honored Contributor

Re: Help with script to edit a text file

Bob,

You could try this -

sed 'N;/^DISK/ s/^DISK.*\]//;s/\n//;N;s/\n//;'

- ramd.
HPE Software Rocks!
Ramkumar Devanathan
Honored Contributor
Solution

Re: Help with script to edit a text file

Here's the correction with command to remove the minus signs -

sed 'N;/^DISK/ s/^DISK.*\]//;s/\n//;N;s/\n//;' | sed 's/-//g'

HTH.

- ramd.
HPE Software Rocks!
john korterman
Honored Contributor

Re: Help with script to edit a text file

Hi,
please try the attached script, using your infile as par 1.

regards,
John K.
it would be nice if you always got a second chance
Rodney Hills
Honored Contributor

Re: Help with script to edit a text file

I think this perl script can do it-

perl -n -e 's/^DISK[^\]]*\]//;if (s/ -$//) { print $_ ; } else { print $_,"\n"; }' infile >outfile

Where infile contains the text and outfile will be the result.

HTH

-- Rod Hills
There be dragons...
Robert Krebs
Occasional Advisor

Re: Help with script to edit a text file

Outstanding!
Thank you all for your helpful answers. I used yours first, Ramkumar, because it was the shortest and it worked like a champ! The others looked great as well. After several days of pounding my head against the wall for this one I finally have an answer. As a good friend says "There's a lot of pleasure to be gained by stopping beating one's head against the wall". Again, thank you for your help, it's why we come here.
Have a grand day, Bob
Admission of wrongdoing is not an admission of weakness, but a sign of strength - Author Unknown
Seth Parker
Trusted Contributor

Re: Help with script to edit a text file

I couldn't help but add another one. Coding sed is just like that...

sed '/-[ ]*$/N;N;s/-[ ]*\n//g'

It seems to work fine and gets rid of the "- " as well.

Regards,
Seth
Seth Parker
Trusted Contributor

Re: Help with script to edit a text file

Bob,

A correction. I had copied the input from the ITRC and noticed that your spec said "-" then linefeed. What I posted handles any trailing spaces that might be there.

If there aren't any trailing spaces, then use this:

sed '/-$/N;N;s/-\n//g'

It's even shorter!

Regards,
Seth