Operating System - Linux
1753974 Members
6795 Online
108811 Solutions
New Discussion юеВ

character counting an manipulation

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

character counting an manipulation

hello all,]

I am working on a task to:

count each character in each row then if the row is over 48 characters log that row to a new file and delete all characters after 48:

so I want to use awk to initally deterrmine what rows are over 48 characters but cannot get the syntax:

awk '{nc = nc + lenght($0) +1 } {if( nc > 48 ) print nc}'

any help please?

chris.
hello
6 REPLIES 6
Dennis Handly
Acclaimed Contributor
Solution

Re: character counting an manipulation

The following echos the truncated lines along with the lines <= 48 bytes.
Lines over 48 bytes are sent to bad_row:
> bad_row # empty file
awk '
{
len = length($0)
if (len > 48) {
print $0 >> "bad_row"
$0 = substr($0, 1, 48) # chop to 48
}
print $0
} '
lawrenzo_1
Super Advisor

Re: character counting an manipulation

i have also attempted to get the sum of the number of characters in each filed with no avail:

awk '{ sum = 0; for (i = 1; i <= NF; i = i +1) sum = sum + $i; print sum}'

Thanks
hello
lawrenzo_1
Super Advisor

Re: character counting an manipulation

Thanks Dennis,

I should have mentioned that this statement is being run on system V unix (doh)

I run the script on system V as it is and get:

16004409190000000100000000000019058051-001
160044092100619510300000000000190580014144
160044092305516610300000000000190580014145
160044092561568811100000000000190580014146
160044092782197010000000000000190580014147
160044092901235110000000000000190580014148
160044093103418510000000000000190580014149
16004409334364 10300000000000190580014150
160044093529527910500000000000190580014151
16004409372859 10200000000000190580014152
16004409394836 10800000000000190580014153
160044094101939810000000000000190580014154
160044094392 10000000000000190580014155
160044094501982810000000000000190580014156
16004409474463 10200000000000190580014157
awk: can't set $0
record number 16

so the code works and bad_row file has the bad data however for some reason when:

$0 = substr($0, 1, 42)

the code fails - is anyone aware of the correct syntax for this?

Chris.
hello
James R. Ferguson
Acclaimed Contributor

Re: character counting an manipulation

Hi Chris:

> I have also attempted to get the sum of the number of characters in each filed with no avail:

Use 'length'. For example:

# echo "here is one"|awk '{for (i=1;i<=NF;i++) {print length($i)}}'

4
2
3

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: character counting an manipulation

$0 = substr($0, 1, 42)
>the code fails - is anyone aware of the correct syntax for this?

I assume if you have a lemon awk, you can use nawk?

Otherwise use another variable:
record = $0
if (len > 48) {
print $0 >> "bad_row"
record = substr($0, 1, 48) # chop to 48
}
print record
lawrenzo_1
Super Advisor

Re: character counting an manipulation

thanks guys again for the expamples.

Dennis you were correct and nawk worked a treat.

And james

Thanks for your input - this has helped me solve the next part of my script.

:-)

Chris
hello