- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Korn Shell - reformat input file
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
Forums
Discussions
Discussions
Discussions
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
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
01-22-2008 02:47 PM
01-22-2008 02:47 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2008 06:23 PM
01-22-2008 06:23 PM
SolutionIt'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 '%
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2008 08:07 PM
01-22-2008 08:07 PM
Re: Korn Shell - reformat input file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2008 09:21 PM
01-22-2008 09:21 PM
Re: Korn Shell - reformat input file
>> 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2008 12:07 AM
01-23-2008 12:07 AM
Re: Korn Shell - reformat input file
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