appending spaces to first line based on second record.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting appending spaces to first line based on second record.
# 1  
Old 11-14-2007
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  
Old 11-14-2007
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  
Old 11-16-2007
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending CRLF to end of record

I need to append |\r\n (a pipe character and CRLF) at end of each record in Unix to all records where they are not already present. So first check for the presence of |\r\n and if absent append it else do nothing (3 Replies)
Discussion started by: abhilashnair
3 Replies

2. Shell Programming and Scripting

Appending ErrorCodes to the corresponding error record

Hi, Here i'm using a awk inside bash script to validate the datafile by referring to the configuration file(schema file). Here the validation check is done for datatype, field length and null values. Once the validation is done on data file the error records are moved to the bad file. So... (22 Replies)
Discussion started by: shree11
22 Replies

3. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

4. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

6. Shell Programming and Scripting

Removing spaces from record

HI i have record as shown below 402665,4X75,754X_FERNIE BC,12F2,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4X75,754X_FERNIE BC,F212,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4Y75,754Y_FERNIE BC,22F2,008708,FERNIE BC,1,UTC ,UTC ,250 here i want to remove multiple spaces into no... (3 Replies)
Discussion started by: raghavendra.cse
3 Replies

7. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

8. Shell Programming and Scripting

Adding spaces to record

Hi, I want to print spaces in a trailer record which is a single command. namely the unix command which i already have recs=`wc -l $TargetFileDir/myfile.txt|cut -c1-9`;export recs;echo 'PCPC.DXDINPT.FC0.INPUTFLE.PASS'`date +%Y%m%d``printf '%015d\n' $recs` >> $TargetFileDir/myfile1.txt I... (3 Replies)
Discussion started by: nvenkat010
3 Replies

9. Shell Programming and Scripting

appending spaces to a variable

Hi All, I have a requirement, in which i have to append some spaces to the variable, and then send it to another function. I am new to the UNIX shell programming. Ultimately the length of the string should be 40 characters. exp: Login = "rallapalli" (length = 10) i have to append 30 spaces to... (2 Replies)
Discussion started by: rallapalli
2 Replies

10. Shell Programming and Scripting

Inserting spaces in a record

Hi all. I am using /bin/sh on an HPUX system. I have a file, with records as such: 60701006000000030380000000000000030380000400000000000 61001006000000008220000000000000008220000100000000000 61201006000000030150000000000000030150001000000000000 I know the character counts which... (5 Replies)
Discussion started by: lyoncc
5 Replies
Login or Register to Ask a Question