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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
appending spaces to first line based on second record. ammu Shell Programming and Scripting 2 11-16-2007 03:35 AM
adding spaces to a line mgirinath Shell Programming and Scripting 4 03-23-2007 12:38 PM
To Trim spaces at the end of line sbasetty Shell Programming and Scripting 1 01-31-2007 10:01 PM
Append strings with filler spaces handak9 UNIX for Dummies Questions & Answers 1 08-11-2005 10:51 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-29-2008
prathima prathima is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
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 to append spaces to the new file.However the data in each line can have multiple spaces in between and I am losing those extra spaces while copying the line into another file.

For eg:
while read line
a=${#line}
do
if ((a==100))
echo $line >> tempfile
fi
done<sourcefile

The problem with the same code is its suppressing multiple spaces into one space if the line has multiple spaces.

Please help
  #2 (permalink)  
Old 04-29-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this one in awk, it appends 10 spaces to lines with a length of 100 chars without changing other lines:

Code:
awk 'length==100{print $0 "          ";next}1' file
Regards
  #3 (permalink)  
Old 04-29-2008
prathima prathima is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
Thanks a ton!

It helped..
Thank you once again!
  #4 (permalink)  
Old 01-27-2009
govindraj govindraj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
TO append Blank Spaces

Hi ,

i need awk command for which it appends 100 blank spaces for Header (starts wirh H..) and Trailer (starts with T..) in a file.

HODACC(need to add 100 blank spaces here)
D0000...
D0000.....
..
..
T000013(need to add 100 blank spaces here)

thanks...
  #5 (permalink)  
Old 01-27-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
it sounds like you want a variable length record file turned into a fixed width record file.

Here's a general awk script you might be able to tweak:

awk '{ printf( "%-106.106s\n", $0 ); }' file_in > file_out

You can add search strings so it's more selective, if you need to.
But it won't hurt on records that are already the right length.
  #6 (permalink)  
Old 01-28-2009
govindraj govindraj is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 6
thanks for reply.....

I am new to unix....
I require to append 100 spaces(fillers) to header(first line) and 100 space to trailer(last line).
the data in the middle not to be changed.

i do not want variable length record file turned into a fixed width record file. i just need to append 100 fillers in the first line and 100 fillers in last line

sample file:

HODA(need to add 100 fillers(spaces) here)
D0000000000000000000000000000000000
D0000000000000000000000000000000000
T000012(need to add 100 fillers(spaces) here)
  #7 (permalink)  
Old 01-28-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try something like this:

Code:
awk -v n=`wc -l < file` '
BEGIN{s=sprintf("%-100s", "")}
NR==1{$0=$0 s)}
NR==n{$0=$0 s)}
1' file
Regards
Sponsored Links
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 02:48 AM.


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