string of 7 char length always...


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers string of 7 char length always...
# 1  
Old 04-12-2005
string of 7 char length always...

Hi,
I know, particular value in the variable should always be of lenth 7 , but the value that is present in thevariable might be of any no.of characters less than or equal to 7... if the no.of characters in the variable is less than 7, I want to add, zeroes at the starting of the field.. How can this be done...

variable
current value It has to be
7 0000007
123 0000123
7654321 7654321

Let me know, how this can be achieved easily
# 2  
Old 04-12-2005
Hammer & Screwdriver

you can proably use printf for formatting your data.

awk '{ printf("%07d",$1)}' data
# 3  
Old 04-12-2005
In ksh, just do
typeset -Z7 var
and then use var. ksh will add the zeroes for you.
# 4  
Old 04-12-2005
You could try exploring the printf command as well. If you have experience using printf in C, you will be right at home. Else, there is always man printf.

Cheers!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. Programming

How to find length of string and pass into char array in C?

Hi All I want to take a Hexadecimal number as input and i want to find lenth of the input and pass it to char s ( char s ). I have a program to convert hexadecial to binary but it is taking limited input but i want to return binary number based on input. How? (1 Reply)
Discussion started by: atharalikhan
1 Replies

3. Programming

PERL \c char in the string

Hi guys, I am stuck up in a situation. I have a SUN box with certain logs which I need to parse to draw a report using Perl. Now, when I load the text file using a perl degugger to see how the text looks like when the first line of the log file is read in a variable. below is the snapshot of... (2 Replies)
Discussion started by: Asteroid
2 Replies

4. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

5. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

6. Shell Programming and Scripting

Parsing char string

I am stumped! I need to parse an input parameter to a script that has the form '-Ort'. I basically need 'O', 'r' and 't', i.e. the individual characters in the string parsed. Since there are no delimiters, I don't know how awk could do this. Can someone tell how to do this, this should be a... (5 Replies)
Discussion started by: ALTRUNVRSOFLN
5 Replies

7. Shell Programming and Scripting

last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also) each line of the file is a fullpath of a file. that makes the list huge. i need to add a functionalitie to that script, i have to be able to add /usr/* or /usr/ and with that reference all the files and folders... (6 Replies)
Discussion started by: broli
6 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. Programming

Compare Char to String

This is actually a c++ question... Basically I am creating a program that asks for five characters. I have a dictionary file containing tons of words no long than five letters long, on a seperate line. I want to be able to take the five inputted letters and compare them to the words in the file... (3 Replies)
Discussion started by: Phobos
3 Replies
Login or Register to Ask a Question