AWK limit for (length) function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK limit for (length) function?
# 1  
Old 05-02-2009
AWK limit for (length) function?

I am trying to use the following code:

awk '{s=$0;if(length(s) < 750){getline; s=s " " $0}printf("%s\n",s)}' filename

but an error shows that 'awk' is too long. Is there a limit to the awk length function? and what could be an alternate solution for long fixed width records?

The code removes a \n (new line feed) within a record without removing the record delimter.

Thanks!
# 2  
Old 05-02-2009
Yes, awk has limitation on the number of chactacters of 3000 characters or something which is the maximum input record size.
However gawk and mawk doesn't seem to have such limits. It can hold upto the max value of C long

So try using gawk instead of awk

Code:
gawk '{s=$0;if(length(s) < 750){getline; s=s " " $0}printf("%s\n",s)}' filename

cheers,
Devaraj Takhellambam
# 3  
Old 05-03-2009
Another way:
Code:
nawk 'length($0) < 750 {s=$0;getline;print s, $0}' file

# 4  
Old 05-03-2009
Quote:
Originally Posted by CKT_newbie88
I am trying to use the following code:

awk '{s=$0;if(length(s) < 750){getline; s=s " " $0}printf("%s\n",s)}' filename

but an error shows that 'awk' is too long. Is there a limit to the awk length function? and what could be an alternate solution for long fixed width records?
An alternate solution for long records could be perl. Unlike *awk, it does not impose any limitation on size of input records. You are only limited by the memory in your system.

Code:
$
$ cat input.txt
12345abcdeMy
name
is
Jack SmitSVY01
23456fghijMy name is
John SmitSVY02
34567klmnoMy name is Cole SmitSVY03
45678pqrstMy name is
Dave
SmitSVY04
56789uvwxyMy name is Eric
SmitSVY05
$
$ perl -ne '{
>   if (length($_) < 36) {chomp; $buf=$buf." ".$_;
>     if (length($buf) == 36) {print substr($buf,1),"\n"; $buf="";}}
>   else {print}
> }' input.txt
12345abcdeMy name is Jack SmitSVY01
23456fghijMy name is John SmitSVY02
34567klmnoMy name is Cole SmitSVY03
45678pqrstMy name is Dave SmitSVY04
56789uvwxyMy name is Eric SmitSVY05
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Function check_badges($color, $uid, $limit = 300, $_DEBUG = true)

Here is the first draft PHP function to check badges: <?php function check_badges($color, $uid, $limit = 300, $_DEBUG = true) { /* * check_badges() version 0.1 by Neo 9 Jan 2019 * $_COOKIE is not used in this server-side code * but may be used in the browser. ... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Limit string length with sed

Hi. I'm trying to learn sed. I want to limit the length of a string. I tried this: sed -n 's/^\(...........................\).*/\1/p' textfile.txt This works fine, but how do I give it as a range rather than putting a hundred dots? I'd like to limit the number of characters to 144 for... (3 Replies)
Discussion started by: troglodytes
3 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

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. Cybersecurity

~ IPTables : Limit Incoming UDP Packets With a Certain Length ~

Hello, I am currently trying to limit incoming UDP length 20 packets on a per IP basis to 5 a second using IPTables on a Linux machine (CentOS 5.2). Basically, if an IP is sending more than 5 length 20 UDP packet a second to the local machine, I would like the machine to drop the excess... (1 Reply)
Discussion started by: tomboy123
1 Replies

7. UNIX for Dummies Questions & Answers

limit of command length

Hi! Can you please help me with one question? Does rexec command have some limitation of the length of the deliveded cmd? Thanks in advance, Anta (2 Replies)
Discussion started by: Anta
2 Replies

8. AIX

Is the Length of User ID for AIX Limit to 8 Characters?

Hi, I'm using AIX version 5.3 currently. I'm trying to create a user id, e.g. andyleong, which the system prompted the length is too long. 1. I would like to know is that the length of user id is limited to maximum 8 characters for AIX. 2. Is it apply to all versions of AIX? If no... (2 Replies)
Discussion started by: meihua_t
2 Replies

9. UNIX for Advanced & Expert Users

length function

Hi,:cool: can any body tell me what is the function to get the length of a string in unix?I have used functions like len,length,strlen, but they are not working. cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 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