Learn how to a build a cloud-first strategyRegister Now


Find region of text in text file with AWK?

Posted on 2006-05-09
Medium Priority
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 46

Expert Comment

by:Kent Olsen
ID: 16642912
Hi cybrthug,

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

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

Good Luck!

Expert Comment

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

Author Comment

ID: 16650025
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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 27

Expert Comment

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

Author Comment

ID: 16661681
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

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

Author Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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