find max length of characters in a perticular field
can u tell me how to find max length of characters in a perticular field.
my input file is s.no,sname 1,sd 35,jtud here max length is 2 right .. i want output is s.no,sname 01,sd 35,jtud like this .. here we just add the zero's before number if <2.. plz help me/... |
The title of your post seems to indicate that you want to find the largest number of characters in a column/field, while in the post itself you seem to want to pad a numeric field with leading zeroes. Which is it?
|
Quote:
We will NOT write your scripts for you, but will be happy to help. |
This snippet doesn't solve the problem but provides an idea to guide you.
With this comma-delimited InFile ... Code:
audi,bentley,bmw Code:
awk -F, '{for (j=1;j<=NF;j++) {print "In record",NR," field",j, Code:
In record 1 field 1 name=audi length of name= 4 |
Quote:
|
Hi Daniel B. Martin,
I don't want to display length of the every field, Quote:
it check the every line and if it is <9 then add 0(zero) before the first field. the Out-file is: Quote:
u got my point right,.. |
So we get your point, where is your attempt to either alter this script to do as you require or your own script that attempts to do the same?
As said earlier, we are not here to write the scripts for you. |
Do you have any previous experience with creating scripts? As grail said, users in this forum will be happy to help you debug and develop a script, but we'd rather not just write one for you.
This is a general "Programming" forum, and your problem could be solved with many different programming/scripting languages. It would be helpful to know which one you'd prefer. (People just looking for a working solution rather than help with solving the problem, should preferably pay someone to create and implement that solution.) |
Quote:
You've been offered a great hint/solution, but have shown zero effort of your own to implement it, or think about how to modify it to do what you want. Show some effort, and we can help. Show no effort, and there's no point in posting. |
I don't have experience to write scripting, i am Beginner.
I tried the code is: Quote:
|
OK, from the snippet you posted it seems that you want to do this:
Also, it seems you want the script to process only the most recent .csv file in a given directory, is that so? |
exactly .. that is only..
|
Quote:
A previous post showed OP how to use length(string). This post goes one step further, showing how to "left pad" a field with length(string) and substr(string)... yet refrains from writing the script for him. Let him read, digest, and adapt this code to his application. With this InFile ... Code:
audi,bentley,bmw Code:
awk -F, '{for (j=1;j<=NF;j++) { Code:
In record 00001, field number 1 contains audi |
Right, then. First, you can indeed get the most recently modified file with ls -lrt | tail -n 1 (or ls -lt | head -n 1 for that matter), but there's really no need to invoke perl when cut can do the job just as well:
Code:
!/bin/sh The remainder of your script is written in awk, a language I'm sadly unfamiliar with, but I'll do my best. The first part of the program attempts to check if the length of field 1 is greater than a variable called "max" and updates the variable if it is, but the second part just prints the input line verbatim. The problem with the current program is twofold:
Code:
max=`awk -F, ' Since the entire thing is in backticks (`), the output is captured by the shell and stored in a shell variable also (confusingly) called "max". You can use this inside a second awk program as $max, as long as you use double quotes instead of single quotes around the awk program, AND escape all other dollar signs (like this: \$). Edit: A much better way would be to simply end the quoting right before the shell variable and restart it afterwards, like this: awk '{print "'$variable'" }'. *goes back to reading awk articles and howtos* Now try and create the second part of the program. :) |
All times are GMT -5. The time now is 08:18 PM. |