AWK - Line length validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - Line length validation
# 1  
Old 09-29-2009
AWK - Line length validation

Hi,
I have requirement where record length is stored in an variable RECORD_LENGTH
Code goes as follows

Code:
RECORD_LENGTH=537
the above is arrived from 
#RECORD_LENGTH=`awk -F"=" '{$sum+=$2} END{print $sum}' DBP_Claims_CFG.ini`

awk '
{
   if (length() == '$RECORD_LENGTH')
     print FNR, $0 > "valid.file";
   else
     print FNR, $0> "invalid.file"
} ' test_file.dat

the above code does not work.
if i try manually like

Code:
awk '
{
   if (length() == 537)
     print FNR, $0 > "valid.file";
   else
     print FNR, $0> "invalid.file"
} ' test_file.dat

code works perfectly.

need to work with variable storing the record length of a file.

your help is appreciated.

Last edited by vgersh99; 09-29-2009 at 12:41 PM.. Reason: code tags, PLEASE!
# 2  
Old 09-29-2009
It can not substitute variables inside single quotation marks like:

Code:
'$RECORD_LENGTH'

Either use double quotation marks (") or hand over shell variables to awk with -v.
# 3  
Old 09-29-2009
Code:
RECORD_LENGTH=537
# the above is arrived from 
RECORD_LENGTH=`awk -F"=" '{sum+=$2} END{print sum}' DBP_Claims_CFG.ini`
awk '
{
   if (length == rec)
     print FNR, $0 > "valid.file";
   else
     print FNR, $0> "invalid.file"
} ' rec="${RECORD_LENGTH}" test_file.dat

# 4  
Old 09-29-2009
Thanks for your help.

i used expr before assigning the RECORD_LENGTH and it worked
RECORD_LENGTH= expr `awk -F"=" '{$sum+=$2} END{print $sum}' DBP_Claims_CFG.ini`;
# 5  
Old 09-29-2009
Quote:
Originally Posted by ainuddin
Thanks for your help.

i used expr before assigning the RECORD_LENGTH and it worked
Code:
RECORD_LENGTH= expr `awk -F"=" '{$sum+=$2} END{print $sum}' DBP_Claims_CFG.ini`;

Code:

Don't quite understand why you need 'expr' and why you have '$sum' instead of plain old 'sum'.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

2. Shell Programming and Scripting

Length validation

I am using below code to find the length of first column additionally I want the complete row which length is greater than 12.at the end I want the rows where first column data length is greater than 12. Just it should validate and highlight the rows where length(first column) is greater than... (5 Replies)
Discussion started by: srivalli
5 Replies

3. Shell Programming and Scripting

Datatype and length validation

I have sourcefile and structure of source file,i want to check whether datatype and length mention in emp.txt is same as source file. Example: in emp.txt first row contains sno number so in source file also first column should contain only number if data is other than number then that... (1 Reply)
Discussion started by: katakamvivek
1 Replies

4. Shell Programming and Scripting

Check for length which exceeds specified length in a line

Hi, I have a issue, I need to loop through a comma delimited file and check for the length which exceeds specified length , if Yes truncate the string. But my problem is , I do not have to check for all the fields and the field lenght is not same for all the fields. For ex: Say my line... (9 Replies)
Discussion started by: rashmisb
9 Replies

5. Shell Programming and Scripting

Command line user input validation

Hi guys, I have a piece of snippet below which asks the user to input some numbers if isDatasubEnabled && isReconEnabled; then echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit" elif isDatasubEnabled &&... (4 Replies)
Discussion started by: pyscho
4 Replies

6. Shell Programming and Scripting

awk date and time validation

How can i validate if user inserted date and optionly a time is vaild but in awk scripting? (18 Replies)
Discussion started by: tal
18 Replies

7. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

8. Shell Programming and Scripting

AWK - Line length validation

I am attempting to use awk to validate the length of each line in a fixed-width raw data file prior to reading in the data. I am happy with.... {if (length() == 403) print > "valid.file"; else print > " invalid.file" } ...to separate out the valid length lines from invalid length... (3 Replies)
Discussion started by: en7smb
3 Replies

9. Shell Programming and Scripting

Command line inputs validation

Hi, I have a script called read.sh that takes a file as input. Now I want to make that script take the file as input with a -f option preceding the filename. How can I do this validation. How can I find whether the given option is -f or not inside the script. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

10. UNIX for Advanced & Expert Users

awk data validation

Hi , This is a general doubt.... Is there any way to do data validation inside an awk script.. Let me make it more clear.... I have a string variable inside awk script .. Is there any way to check whether it is number or string etc... Thanks in advance. Shihab (1 Reply)
Discussion started by: shihabvk
1 Replies
Login or Register to Ask a Question