AWK - Line length validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK - Line length validation
# 1  
Old 05-09-2007
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 lines.

However, I need to extend this to add the line number as read from the original file onto the start of each line before writing out. There are potentially tens of millions of records in a file, so lets say the line number occupies the first 8 characters of a line, making the total length (of valid lines) 411.

Any help greatly appreciated,

Thanks,

Steve
# 2  
Old 05-09-2007
Code:
{
if (length() == 403)
   printf("%08d%s\n", FNR, $0) > "valid.file";
else 
   printf("%08d%s\n", FNR, $0) > " invalid.file"
}


Jean-Pierre.
# 3  
Old 05-09-2007
Code:
{
   if (length() == 403)
     print FNR, $0 > "valid.file";
   else
     print FNR, $0> " invalid.file"
}

# 4  
Old 05-09-2007
Like it.

Thanks a lot.

Steve
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 - Line length validation

Hi, I have requirement where record length is stored in an variable RECORD_LENGTH Code goes as follows 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... (4 Replies)
Discussion started by: ainuddin
4 Replies

7. 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

8. 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

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