- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- character counting an manipulation
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 08:45 PM
тАО09-24-2007 08:45 PM
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.
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 09:08 PM
тАО09-24-2007 09:08 PM
SolutionLines 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
} '
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 09:08 PM
тАО09-24-2007 09:08 PM
Re: character counting an manipulation
awk '{ sum = 0; for (i = 1; i <= NF; i = i +1) sum = sum + $i; print sum}'
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 10:19 PM
тАО09-24-2007 10:19 PM
Re: character counting an manipulation
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 10:27 PM
тАО09-24-2007 10:27 PM
Re: character counting an manipulation
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 10:35 PM
тАО09-24-2007 10:35 PM
Re: character counting an manipulation
>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
- Tags:
- nawk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2007 11:23 PM
тАО09-24-2007 11:23 PM
Re: character counting an manipulation
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