LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-18-2012, 07:51 AM   #1
mfender
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
Shell Scripting Help


I'm new, and I wasn't sure if this was the correct thread to post this in. And if this isn't, I would be very appreciative if it was moved into the correct thread.

I am having a problem figuring out how to tackle this problem. I'm currently trying to go through some of my log files to copy out a night's import into a different file. There would be two keywords that would signify the start and end of the section I would copy out. Or, rather, the "keywords" would be a date on its first appearance into the text file. I couldn't find something on google to help me figure out the correct method of putting this in a shell script. Help?

Thank you.
 
Old 01-18-2012, 07:58 AM   #2
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
awk will sure do the job.
 
Old 01-18-2012, 08:00 AM   #3
mfender
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Okay! But, I'm not entirely sure how to use awk... or, rather, I've read it, but I'm not entirely sure how to use it. May I get some help with that?
 
Old 01-18-2012, 08:17 AM   #4
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
Lets assume you have a file containing:

Code:
one   two    three
four  five   six
seven eight  nine
ten   eleven twelve
A     B      C
D     E      F
And you want to start copying when you find a line containing five in the second field and stop when you find C in the third field (but not including those lines) the command:
Code:
awk 'BEGIN{x=0}{if ($3=="C") {x=0};if (x) {print};if ($2=="five") {x=1}}' file
will do the job.

This will only work assuming the second condition never appears before the first condition, which is what I have understand from your first post. Anyway, its something rough as an example.

BEGIN clause will only execute once at the begining of awk. The rest of the code will be executed for every line read from file.

Last edited by rodrifra; 01-18-2012 at 08:27 AM.
 
Old 01-18-2012, 08:23 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
awk would do it but sed is better suited to the task.

Use Ranges by patterns to define the lines you want to extract and p to print them.
 
Old 01-18-2012, 12:48 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Member response

Hi,
Welcome to LQ!
Quote:
Originally Posted by mfender View Post
I'm new, and I wasn't sure if this was the correct thread to post this in. And if this isn't, I would be very appreciative if it was moved into the correct thread.

I am having a problem figuring out how to tackle this problem. I'm currently trying to go through some of my log files to copy out a night's import into a different file. There would be two keywords that would signify the start and end of the section I would copy out. Or, rather, the "keywords" would be a date on its first appearance into the text file. I couldn't find something on google to help me figure out the correct method of putting this in a shell script. Help?

Thank you.
Other members have given some good advice. I will suggest these links to aid you to gaining some understanding. Sure some topics may seem beyond a newbie but you must start somewhere;



Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Bash Beginners Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking



The above links and others can be found at '
Slackware-Links'. More than just SlackwareŽ links!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration