LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Scripting Help (https://www.linuxquestions.org/questions/linux-general-1/shell-scripting-help-924414/)

mfender 01-18-2012 07:51 AM

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.

rodrifra 01-18-2012 07:58 AM

awk will sure do the job.

mfender 01-18-2012 08:00 AM

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?

rodrifra 01-18-2012 08:17 AM

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.

catkin 01-18-2012 08:23 AM

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.

onebuck 01-18-2012 12:48 PM

Member response
 
Hi,
Welcome to LQ!
Quote:

Originally Posted by mfender (Post 4577812)
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!


All times are GMT -5. The time now is 12:48 AM.