Find region of text in text file with AWK?

Posted on 2006-05-09
Last Modified: 2008-02-01
I have a text file that contains the area SECTION: A45345234DJAA ASD 234

What I need is the information after SECTION: which changes with each text file.

Currently I can use the following to get all the fields after it but I only want to get up to 20 characters past the SECTION: area so I dont
get any info I dont need since there is other text after the actual SECTION: entry. Here is what I have but I need it changed to only look for up to 20 characters of text after the colon:

`awk '/SECTION: .*/{print $2 $3 $4 $5}' $FILE`

Thanks in advance!
Question by:cybrthug
    LVL 45

    Expert Comment

    Hi cybrthug,

    If everything is on one line, pipe the output to cut.

    grep SECTION [filename] | cut -d':' -b20

    Good Luck!
    LVL 6

    Expert Comment

    grep SECTION:  $file  |cut -f2 -d' '

    Author Comment

    kdo, yours doesn't see a field. bira yours works but only limits it to one single field, i have white spaces in the section, so i need to count
    at least 20 characters within that field, otherwise it thinks its more then one field.

    SECTION: A45345234DJAA ASD 234

    If going by this SECTION: bira's example would only pick up A45345234DJAA. I need the whole string from this section.

    LVL 27

    Expert Comment

    sed '/SECTION:/s/^.*SECTION: *//p;d' $FILE

    Author Comment

    Nopius, that gives me the entire line that SECTION resides on unfortunately. Anybody figure out how to get up to all 20 characters after SECTION: ?

    LVL 27

    Accepted Solution

    Do you need _exactly_ 20 characters after SECTION: excluding leading spaces?
    sed '/SECTION:/s/^.*SECTION: *//p;d' $FILE | cut -b1-20

    Author Comment

    Excellent! Thank you! This will help me tremendously with parsing my files.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
    In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Video by: Phil
    This video goes over how to configure and start a jail in FreeBSD.  This video is meant to supplement the article included with this course.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now