The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2007
ammu ammu is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 79
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.
  #2 (permalink)  
Old 11-14-2007
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
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
Usage: scriptname your_file

Regards

Last edited by Franklin52; 11-14-2007 at 03:27 PM.. Reason: Use parameter
  #3 (permalink)  
Old 11-16-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
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
output:
Code:
a              *
b         b   *
c    e        *
dd            *
e a fd       *
dd            *
code:
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0