Operating System - HP-UX
1833870 Members
1689 Online
110063 Solutions
New Discussion

Re: Korn Shell - reformat input file

 
SOLVED
Go to solution

Korn Shell - reformat input file

Hi,

Anybody please help me.

I have input file as below :-
ADIL LM 940328106109 MUHAMMAD ASYRAF B AZMAN
ADIL LM 940923105071 MUHAMMAD DANIAL NASIRUDDIN
ADIL LM 940818146028 MUHAMMAD FAIZAL B MOHD FAUZI
ADIL PM 940607146234 NOR SYAKINA FARA BT MOHAMAD
ADIL PM 940715105480 NORSHAFIKA BT SULAIMAN
ADIL PM 14494 NUR AZIRA BT DARNALIS
ADIL PM 940119145670 NUR AZIRA BT MOHD JANI
ADIL PM 940729105596 NUR FARZANA BT MOHD KAMSAINY

1st field = Class
2nd field = Code
1st & 2nd field length are (fix).

3rd field = SocialNo (length not fix, 4 to 12 characters)
4rd field is a name (length not fix)

My output file format as per attached.
1st field = "BEA46112" (fixed)
2nd field = Class & Must start at position 11.
3rd field = Name & Must start at position 21.
4rd field - SocialNo & Must Start at position 51.
5th field = Code & Must start at position 64.
6th fiels = "0 0 2008" (fixed) & Must start at position 68
of the output file.

Kindly help how could I format the input file to get the formated output file as per attached through korn shell.

TQ
4 REPLIES 4
Hein van den Heuvel
Honored Contributor
Solution

Re: Korn Shell - reformat input file

TQ,

It's refreshing to see clear description of input and output. Thanks!
There is a little discrepancy in that the name in the output seems to start in column 20, not 21 as indicated.

Here is a perl solution, which you can stick in a korn shel script if you must:

$ perl -ne 'printf ("%-10s%-10s%-30s%-13s%-4s0 0 2008\n","BEA46112",$1,$4,$3,$2) if /(\w+)\s+(\w+)\s+(\d+)\s+(\S.*)/' x

The printf force the output to be left justified (the '-' in fixed length character fields '%s' where is the width. I even used this for the fixed text, to avoid typos & counting.
Ths print arguments come from a matching expression which looks for recognizable parts of the string \w+ for a word, \s+ for whitespace and so on.

Enjoy!
Hein.


Re: Korn Shell - reformat input file

Thanks HEIN.

Greats & it works.

If I to mix the "BEA46112" with 1st Field ($1) without space in between and my output as follow :-

BEA46112ADIL ...so on

What the code I should change?

The follow code :
perl -ne 'printf ("%-8s%-10s%-30s%-13s%-4s0 0 2008\n","BEA46112"$1,$4,$3,$2) if /(\w+)\s+(\w+)\s+(\d+)\s+(\S.*)/' x

giving me error
Scalar found where operator expected at -e line 1, near ""BEA46112"$1"
(Missing operator before $1?)
syntax error at -e line 1, near ""BEA46112"$1"
Execution of -e aborted due to compilation errors.

Regards
-Kamarul
Hein van den Heuvel
Honored Contributor

Re: Korn Shell - reformat input file

>> If I to mix the "BEA46112" with 1st Field ($1) without space in between and my output as follow : BEA46112ADIL ...so on

>> What the code I should change?

That would be a minor change to the format.
Instead of fixed width 10 as originally requested, just use a %s for a 'tight' string.

'printf ("%-8s...
'printf ("%s...

Or, for a bigger change, hardcode the text in the format:

$ perl -ne 'printf ("%-10s%-10s%-30s%-13s%-4s0 0 2008\n","BEA46112",$1,$4,$3,$2) if /(\w+)\s+(\w+)\s+(\d+)\s+(\S.*)/' x


becomes

perl -ne 'printf ("BEA46112%-10s%-30s%-13s%-4s0 0 2008\n",$1,$4,$3,$2) if /(\w+)\s+(\w+)\s+(\d+)\s+(\S.*)/' x

Hein.
Dennis Handly
Acclaimed Contributor

Re: Korn Shell - reformat input file

You can take Hein's solution and turn it into awk:
awk '
{
name = $4 # collect variable field name
for (i = 5; i <= NF; ++i) name = name " " $i
printf "BEA46112%-10s%-30s%-13s%-4s0 0 2008\n", $1, name, $3, $2
} ' file