![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to append spaces(say 10 spaces) at the end of each line based on the length of th | prathima | UNIX for Dummies Questions & Answers | 17 | 01-28-2009 04:10 PM |
| Adding spaces to record | nvenkat010 | Shell Programming and Scripting | 3 | 01-28-2008 01:24 PM |
| appending spaces to a variable | rallapalli | Shell Programming and Scripting | 2 | 08-13-2007 01:23 AM |
| Inserting spaces in a record | lyoncc | Shell Programming and Scripting | 5 | 06-01-2007 12:27 PM |
| appending with sed based on matched pattern | jack1981 | Shell Programming and Scripting | 2 | 07-20-2006 06:54 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
appending spaces to first line based on second record.
Hi,
I have a situation to append spaces to end of first record (header)and last record (footer) based on second record length. The first record length is always 20.The second record will be different for different files.I have to append spaces for the first line based on second record length.The length of second record is also known. Similarly for last record.Its length is also 25.i have to append spaces based on second record.The file is fixed width file.So that the record length will be same through out the file. I have to do this for several files... Please advice. |
|
||||
|
Try this:
Code:
#!/bin/sh
nl=`wc -l $1|cut -d" " -f1`
awk '
{
if (NR==1) {
len=length($0)
r=$0
next
}
if (NR==2) {
len2=length($0)
printf("%s",r)
for(i=1;i<=len2-len;i++) {
printf(" ")
}
printf("\n")
}
if (NR=='$nl') {
len=length($0)
printf("%s",$0)
for(i=1;i<=len2-len;i++) {
printf(" ")
}
printf("\n")
}
else
{
print
}
} ' $1
Regards Last edited by Franklin52; 11-14-2007 at 03:27 PM.. Reason: Use parameter |
|
||||
|
awk
Hi guy,
Actually, this code is not totally right on your target. But i hope it will help you. My logic is: No matter how length should be each line, the result will be the longest line. It means every line will be set to the certain length of the longest line of the file. input: Code:
a b b c e dd e a fd dd Code:
a * b b * c e * dd * e a fd * dd * Code:
awk '{
if (length($0)>=n)
n=length($0)
line[NR]=$0
}
END{
for (i=1;i<=NR;i++)
printf("%-"n"s*\n",line[i])
}' a
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|