Length of each line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Length of each line.
# 1  
Old 08-28-2008
Length of each line.

Hi All,

I have 5 different type of lines in my file TEST1.txt

I need to perform the following conditions in the file

If first 4 characters of the line is 1010, then length of the line should be 132
If first 4 characters of the line is 9142, then length of the line should be 41
If first 4 characters of the line is 1011, then length of the line should be 250
If first 4 characters of the line is 9999, then length of the line should be 70
If first 4 characters of the line is 4949, then length of the line should be 152

I am using cut, but it is taking very long time to execute the entire file.

Can anyone help on this.
Thanks in advance
# 2  
Old 08-28-2008
Quote:
Originally Posted by senthil_is
Hi All,

I have 5 different type of lines in my file TEST1.txt

I need to perform the following conditions in the file

If first 4 characters of the line is 1010, then length of the line should be 132
If first 4 characters of the line is 9142, then length of the line should be 41
If first 4 characters of the line is 1011, then length of the line should be 250
If first 4 characters of the line is 9999, then length of the line should be 70
If first 4 characters of the line is 4949, then length of the line should be 152

I am using cut, but it is taking very long time to execute the entire file.

Can anyone help on this.
Thanks in advance
What is the action you want to do if a particular condition is met?
# 3  
Old 08-28-2008
if the condition not met, it should throw the error mesg with line number in seperate file.
# 4  
Old 08-28-2008
i can give you an idea..
first read the file line by line using while loop
then find the length
according to line length write switch case statement where you can check fro first 4 characters
this should take less time..
# 5  
Old 08-28-2008
Apply the logic :

Code:
awk '{
 if (!(/^1010/ && length($1)==132)) print NF" "$0>>"/tmp/file_out" ;
 if (!(/^9142/ && length($1)==41)) print NF" "$0>>"/tmp/file_out" ;
 if (!(/^1011/ && length($1)==250)) print NF" "$0>>"/tmp/file_out" ;
 if (!(/^9999/ && length($1)==70)) print NF" "$0>>"/tmp/file_out" ;
 }'  filename

# 6  
Old 08-28-2008
The logic seems wrong to me, it will print basically every line because some of the conditions will be true for all lines.

Perhaps this is closer to what you want.

Code:
awk '/^1010/ { if (length($0)==132) next }
/^9142/ { if (length ($0) == 41) next }
/^1011/ { if (length ($0) == 250) next }
/^9999/ { if (length ($0) == 70) next }
/^4949/ { if (length ($0) == 152) next }
{ print NF }' filename >/tmp/file_out


Last edited by era; 08-28-2008 at 05:59 AM.. Reason: Missing 4949 case; use "next" instead of print; use length of $0 as per requirements
# 7  
Old 08-28-2008
Quote:
Originally Posted by era
The logic seems wrong to me, it will print basically every line because some of the conditions will be true for all lines.

Perhaps this is closer to what you want.

Code:
awk '/^1010/ { if (length($0)==132) next }
/^9142/ { if (length ($0) == 41) next }
/^1011/ { if (length ($0) == 250) next }
/^9999/ { if (length ($0) == 70) next }
/^4949/ { if (length ($0) == 152) next }
{ print NF }' filename >/tmp/file_out

thanks for pointing it out. Smilie We need to use the else to tackle it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Vim line length...

The vi(m) text editor... Google is not my friend here... I have already found out that it is possible to save a pure text file without the '0x0A' newline character at the very end of the file but I can't find anything on the next question. I have written a raw audio file to quantised pure... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. Programming

Maximum length of a line

How can I change the maximum length of a programming line in fortran and C (specifically in fortran 77)? Seems the default maximum length is 72 in fortran 77. Thanks. (4 Replies)
Discussion started by: hbar
4 Replies

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

4. Programming

Is there a limit for a code line length in C?

Is there any stabdard limitation on size of a code line in C code? I am interesting in UNIX limitation, particulary on SUN. Thanks! (8 Replies)
Discussion started by: alex_5161
8 Replies

5. Shell Programming and Scripting

Count line Length

I am trying to write a shell scrip that can give me the line length of a record that was in EBDIC and then converted to ASCII. Everything I try reports 1 yet the length is 2000+. I have tried echo "Line length : ${#FILE}" echo "FILE" |awk -F, '{print NF}' awk '{lenth(file)}' All I can... (11 Replies)
Discussion started by: wbshrk
11 Replies

6. Shell Programming and Scripting

Parse a line which has different word length

Hi All, Please let me know a command to parse the below line and find the words, I have a line like this 40609 39930 In this above line the two words are separted by space.The length of this two words may differ. I want to put 40609 in var_one and 39930 in var_two. Eg. Input line is ... (1 Reply)
Discussion started by: girish.raos
1 Replies

7. Shell Programming and Scripting

separate file by line length

hi all, i'm new in unix.... i have question, sorry if it's missplace or too silly let say i have a file name testfile.log that contains data 000001 000002 000003 aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb cccccccccccccc dddddddddddddddddd 000004 i want to make new file... (3 Replies)
Discussion started by: venven
3 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

line length

hi In my script i am having echoes which have very lage output. echo $final sample output: etc... The output gets printed continuosly. Is there any way by which I can put a line break after every 25 positions? In nutshell , I need a neat output of my BIG echo as below: (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

10. Shell Programming and Scripting

grep line length limit

Hi Friends, I am having a funny problem with grep. When I run grep 'expr' file.txt things work fine. But when try to get the line number using the -n option, i.e, grep -n 'expr' file.txt I get a message, "grep: 0652-226 Maximum line length of 2048 exceeded." If the line has more than... (3 Replies)
Discussion started by: hnhegde
3 Replies
Login or Register to Ask a Question