sed replace with fixed length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace with fixed length
# 1  
Old 05-14-2008
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?
# 2  
Old 05-14-2008
Code:
sed "s/s.no/1  /" template

# 3  
Old 05-14-2008
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?
# 4  
Old 05-14-2008
Use printf to create the sed script maybe? Or replace everything with three spaces and then overwrite as many as necessary?
# 5  
Old 05-14-2008
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 10:03 AM..
# 6  
Old 05-14-2008
Code:
sed 's/^s\.no//' 65152_noname.d | awk ' BEGIN { ctr=1 } { printf("%-3s %-s\n", ctr, $0); ctr+=1 }'

# 7  
Old 05-15-2008
Quote:
Do you get the numbers from a file?
yes, I do get the number from input data file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Replace and Increment a value in the fixed length file

Hi Everyone, I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this. Ex input file ASDSD ADSD 00000 X AAASD ADSD 00000 X SDDDD ADSD 00000 X Ouput ASDSD ADSD 00001 X AAASD ADSD 00002 X SDDDD ADSD 00003 X ... (7 Replies)
Discussion started by: saratha14
7 Replies

3. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== IXTTIV110827 NANTH AM IKSHIT ABCDEF 0617 IJAY NAND EENIG ZXYWVU 0912 AP OOK OONG PQRSTU100923 NASA DISH TTY ASDFG 0223 GHU UMA LAM QWERT 0111 ATHE SH THEW ======= From 7th to 12 is a date... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

4. Shell Programming and Scripting

sed to replace at fixed location

I have got a text file- each line of 200 characters length. The file is too large in size. It could be 100 MB. The lines begin with any of 0,1,2,3,4,5. I want to replace from 121-131 characters with spaces irrespective of wehatever it is there (the exisitng charatcers could be spaces). And this I... (6 Replies)
Discussion started by: asutoshch
6 Replies

5. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

6. Shell Programming and Scripting

Search and replace particular characters in fixed length-file

Masters, I have fixed length input file like FHEAD0000000001XXXX20090901 0000009000Y1000XXX2 THEAD000000000220090901 ITM0000109393813 430143504352N22SP 000000000000RN000000010000EA P0000000000000014390020090901 TTAIL0000000003000000 FTAIL00000000040000000002 Note... (4 Replies)
Discussion started by: bittoo
4 Replies

7. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

9. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question