grep out and fix a section


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep out and fix a section
# 8  
Old 06-22-2011
Code:
gawk.exe.stackdump

Is this windows?

awk actually crashing on anything is about as normal as your computer hitting you in the face with a boxing glove.
# 9  
Old 06-22-2011
crashes my awk too... i suspect bug in awk...

---------- Post updated at 12:19 PM ---------- Previous update was at 12:11 PM ----------

An additional condition has to be added to check whether there is 9 fields; otherwise calling length() on non-existent 9th field causes memory corruption. This should work:
Code:
awk '{if(NF>8 && length($9)!=12)$9="000000000000" ; print}' sample.txt

---------- Post updated at 12:21 PM ---------- Previous update was at 12:19 PM ----------

well, the input sure is a windows file, since there are \r\n, but it crashes MY GNU awk on linuxbox too Smilie

GNU Awk 3.1.6

Last edited by mirni; 06-22-2011 at 07:27 PM.. Reason: it's length() function not the assignment
# 10  
Old 06-22-2011
HI,

The perl works, but its changing to many zeros Smilie
Code:
# =====================================       000000000000
# Add Scope End     000000000000
# =====================================       000000000000
        000000000000
        000000000000
# =====================================       000000000000
# Add Super Scope     000000000000
# =====================================       000000000000
        000000000000
        000000000000
# =====================================       000000000000
# Add Super Scope End    000000000000
# =====================================       000000000000
        000000000000
# =====================================       000000000000
# Add MScope      000000000000
# =====================================       000000000000
        000000000000
        000000000000
        000000000000
# =====================================       000000000000
# Add MScope End     000000000000
# =====================================       000000000000
        000000000000
        000000000000
# ==============================================================       000000000000
# Configuration Information for Server 10.82.150.13 Ends  000000000000
# ==============================================================       000000000000
        000000000000
        000000000000

I just need it to focus on stuff like this

Code:
Dhcp Server 10.82.150.13 Scope 10.82.253.0 Add reservedip 10.82.253.7  00065be230e4 "NB12875.SAN.LOCAL" "Alejandra  Arboleda/Southyard/Slauson/Dell latitude C610" "DHCP"

AWK version:

Code:
$ awk --version
GNU Awk 3.1.8
Copyright (C) 1989, 1991-2010 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see Licenses - GNU Project - Free Software Foundation.

---------- Post updated at 09:22 PM ---------- Previous update was at 09:18 PM ----------

HI, yes using windows in cygwin

This is working
Code:
awk '{if(NF>8 && length($9)!=12)$9="000000000000" ; print}'

but its changing 0000000000 in sections I do not need. Just need it to focus on stuff only in sections with:
Code:
Dhcp Server 10.82.150.13 Scope 10.82.253.0 Add reservedip 10.82.253.7   00065be230e4 "NB12875.SAN.LOCAL" "Alejandra   Arboleda/Southyard/Slauson/Dell latitude C610" "DHCP"

Thanks

Last edited by Franklin52; 06-23-2011 at 03:46 AM.. Reason: Please use code tags
# 11  
Old 06-23-2011
oops sorry. Yeah, zero is not equal to 12, is it? Here, try this:
Code:
perl -lane '{$F[8]="000000000000" if(length($F[8])>0 && length($F[8]) != 12); print "@F";}' sample.txt

You wanted to change the 9th field, if it was smaller or larger than 12 characters, right?
If you need to change just the shorter ones, just change the logic:
Code:
perl -lane '{$F[8]="000000000000" if(length($F[8])>0 && length($F[8]) < 12); print "@F";}' sample.txt

# 12  
Old 06-24-2011
Hi, I tried it, THANKS !

but somehow its changing more then I need. Could we narrow it down please?

Thanks
# 13  
Old 06-24-2011
Quote:
Could we narrow it down please?
What do you mean? The more specific you are with your requests, the better are your chances of getting help with your issue.
The perl code above works like this:
It processes input line-by-line (-n switch), splits each line on whitespaces and stores the individual fields into @F array (-a switch). Then I am working with the ninth field, $F[8]. If the field is non-empty (its length is > 0 ) and its length is other than twelve, then it's changed to "000000000000". Then the whole array (line) is printed, whether changed or not.

Now what exactly you don't like about this? Maybe you want to change other fields than the ninth field or....? I encourage you to try to modify the code yourself -- that way you'll learn about the tool.

---------- Post updated at 02:20 AM ---------- Previous update was at 02:18 AM ----------

BTW i got a response to the bug report I filed regarding awk crash. Here is the relevant part of it:

Quote:
This is indeed a bug that shows up outside the "C" locale.

You can pull the current gawk code from the git repository on Savannah
(start at http://savannah.gnu.org, put 'gawk' into the Project box
and follow the info for getting the code).

You can check out the gawk-stable branch, which has the problem fixed
and is closer to 3.1.6, or just build off the master branch which also
has the problem fixed.
# 14  
Old 06-24-2011
Thanks for the Bug reply !.

I hear ya about your code. Could it narrow it down once the file is opened to a specific header in the file like:

#
Quote:
======================================================================
# Start Add ReservedIp to the Scope : 10.82.20.0, Server : 10.82.174.206
# ======================================================================
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a section from an UNIX file obtaining only part of the data

Hello, I have a log file that has several sections "BEGIN JOB, End of job" like in the following example: 19/06/12 - 16:00:57 (27787398-449294): BEGIN JOB j1(27787398-449294) JOB1 19/06/12 - 16:00:57 (27787398-449294): DIGIT: 0 number of present logs : 1 19/06/12 - 16:00:57... (4 Replies)
Discussion started by: mvalonso
4 Replies

2. Shell Programming and Scripting

How to Grep of by section?

I have a script that outputs this as a file John Smith ---------------- memberOf: example1;sampletest;test memberOf: example2;sampletest;test memberOf: example3;sampletest;test memberOf: example4;sampletest;test A Member of 4 Groups Sally Smith ---------------- memberOf:... (4 Replies)
Discussion started by: ajetangay
4 Replies

3. Shell Programming and Scripting

Grep text and see all section

Hello I am looking for a way to look in files and to grep text and see the all section of the text ! Sharon123 deed 10000 class 360 ! sharon456 2000 deed ! Sharon789 live 3000 ! To grep "deed "an see the all section (5 Replies)
Discussion started by: sharong
5 Replies

4. Shell Programming and Scripting

Grep or print each section of a file on one line with a separator

I can obtain information from itdt inventory command however it display as below, I'd like to print each entity on one line but seperated by : the file is something like and each section ends with Volume Tag Drive Address 256 Drive State ................... Normal ASC/ASCQ... (3 Replies)
Discussion started by: gefa
3 Replies

5. Shell Programming and Scripting

Prepend first line of section to each line until the next section header

I have searched in a variety of ways in a variety of places but have come up empty. I would like to prepend a portion of a section header to each following line until the next section header. I have been using sed for most things up until now but I'd go for a solution in just about anything--... (7 Replies)
Discussion started by: pagrus
7 Replies

6. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

7. UNIX for Dummies Questions & Answers

how can i extract text section via grep

hi, i have a very long text file. i need to extract with grep command a certain part. for example text file include 1ooo rows: 1.... 2... 3... . . . 1000 i want to view with grep only rows 50-100. any ideas will be appreciated thanks... (8 Replies)
Discussion started by: meny
8 Replies

8. Post Here to Contact Site Administrators and Moderators

New Section

Just like we have a section "Unix for dummies..." , why not have a section on UNIX BACKUP AND RECOVERY Thanks :) (3 Replies)
Discussion started by: kapilv
3 Replies

9. Post Here to Contact Site Administrators and Moderators

New section

What are the chances of possibly posting some of the more common how-to type stuff for the newbies so we can avoid the repititious stuff that appears every other day? Not so much like a Q&A forum, but more like a reference area for the mundane stuff. Beyond searching the forum, I think people... (3 Replies)
Discussion started by: ober5861
3 Replies
Login or Register to Ask a Question