The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade 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 here. Shell Script Page.


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 3 10-19-2007 11:16 AM
Fixed length (Fill out) peterk Shell Programming and Scripting 3 09-28-2007 08:04 AM
Awk with fixed length files c2b2 Shell Programming and Scripting 7 01-06-2007 08:57 AM
fixed length fields in awk roopla Shell Programming and Scripting 2 11-13-2006 05:12 PM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 09:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 18
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
sed replace with fixed length

Quote:
I have template file with content as below:
Code:
$ cat template
s.noName
Quote:
I want to use this template file to generate the data file using sed
Code:
$ sed "s/s.no/1/" template > out
$ sed "s/s.no/100/" template >>out
Quote:
now the content of the out file is :
Code:
$ cat out
1Name
100Name
Quote:
I would like to see the content of the file as below:
Code:
1  Name
100Name
Quote:
The maximum S.No I am expecting is 999, that is for S.no I want to use 3 digits. if the S.no is only one digit I wan to have 2 spaces after the S.no, if the S.no is with 2 digits then I want to have one space after the S.no
How can I modify the sed command to achieve the desired result?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Code:
sed "s/s.no/1  /" template
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Posts: 18
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Thanks for the solution.
If I have to do this for some 100 number of records from input file , and the input file contains the data with s.no.
I will be getting the s.no into variable in the loop and I would like use sed in the loop as below
Code:
sed "s/s.no/$sno/" template >>out
Quote:
I will not be sure for every record what is the length of the $sno, it could be 1 or 2 or 3.

How to handle in this situation?
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Use printf to create the sed script maybe? Or replace everything with three spaces and then overwrite as many as necessary?
Reply With Quote
  #5 (permalink)  
Old 05-14-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,279
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
I will not be sure for every record what is the length of the $sno, it could be 1 or 2 or 3.
How to handle in this situation?
Do you get the numbers from a file?

Regards

Last edited by Franklin52 : 05-14-2008 at 06:03 AM.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB 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 -7. The time now is 09:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102