Sponsored Content
Top Forums Shell Programming and Scripting Grabbing data between 2 points in text file Post 302840001 by Just Ice on Sunday 4th of August 2013 03:43:16 AM
Old 08-04-2013
the problem here is that your input does not have easily delimited paragraphs where sed or awk can quickly work ... it would have been quiet easy to awk "/WR25/,/^$/" sunnywebbox-summary.txt but the empty line immediately after the Device line kills that ... doing the original awk command line i suggested would work if you could just remove the second Device line like in awk "/WR25/,/WRHU/" sunnywebbox-summary.txt | grep -v WRHU but that would only work if you know which device follows in the report and the listed device is not the last in the list ...

anyways, the script below should work with or without an argument ... it really only uses sed to get the desired output but needed all the other lines to determine the sed reference addresses ... somebody here could probably script this in perl or awk better but at least you have something to start with ...
Code:
#! /bin/ksh
PATH=/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin
device=$1
testfile=testfile1

grep -n -i device $testfile > /tmp/$$
if [ ! $device ]
then
    print "\n--- Devices ---"
    awk '{print $4}' /tmp/$$ | awk -F: '{print $1}'
    print "\nWhich device to report? \c"
    read device
fi

linecnt1=$(wc -l < $testfile)
linecnt2=$(wc -l < /tmp/$$)
startcnt=$(grep -n $device /tmp/$$ | awk -F":" '{print $1}')
if [ $startcnt -eq linecnt2 ]
then
    endline=$linecnt1
else
    nextcnt=$(expr $startcnt + 1)
    tempcnt=$(sed -n "${nextcnt}p" /tmp/$$ | awk -F":" '{print $1}')
    endcnt=$(expr $tempcnt - 1)
    endline=$endcnt
fi
startline=$(grep -n $device $testfile | awk -F":" '{print $1}')

echo
sed -n "${startline},${endline}p" $testfile

rm -f /tmp/$$ 2> /dev/null

exit 0

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grabbing filename from text file....should be easy!

Quick question...I'm trying to grab the .tif file name from this output from our fax server. What is the best way i can do this in a bash script? I have been looking at regular expressions with bash or using awk but having some trouble. thanks! The only output i want is... (5 Replies)
Discussion started by: kuliksco
5 Replies

2. UNIX for Dummies Questions & Answers

How to get data only inside polygon created by points which is part of whole data from file?

hiii, Help me out..i have a huge set of data stored in a file.This file has has 2 columns which is latitude & longitude of a region. Now i have a program which asks for the number of points & based on this number it asks the user to enter that latitude & longitude values which are in the same... (7 Replies)
Discussion started by: reva
7 Replies

3. UNIX for Dummies Questions & Answers

Help grabbing data of a link

Hi folks; I have a temperature sensor that configured with an IP address so we can open a browser to see the temperatures in our 3 labs all showing in one screen. I'm trying to write a script to call this link then email me the results/output so i don't have to open a browser manually every... (3 Replies)
Discussion started by: Katkota
3 Replies

4. Shell Programming and Scripting

Calculate difference between consecutive data points in a column from a file

Hi, I have a file with one column data (sample below) and I am trying to write a shell script to calculate the difference between consecutive data valuse i.e Var = Ni -N(i-1) 0.3141 -3.6595 0.9171 5.2001 3.5331 3.7022 -6.1087 -5.1039 -9.8144 1.6516 -2.725 3.982 7.769 8.88 (5 Replies)
Discussion started by: malandisa
5 Replies

5. Shell Programming and Scripting

Grabbing text and using that text in a newly created line

Hello, I am really stuck and I'm hoping somone can help. I have a text file that is similar to this: <--First User--> <function>account='uid=user1,....... <--Second User--> <function>account='uid=user2,.......What I want is to grab the usernames after "uid=" and before the following... (9 Replies)
Discussion started by: mafia910
9 Replies

6. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

7. Shell Programming and Scripting

Grabbing a chunk of text from a file

Hi, I have a Report.txt file. Say the contents of this file are : 1 2 3 4 5 7 df v g gf e r dfkf lsdk dslsdklsdk Report Start: xxxxxxdad asdffsdfsdfsdfasfasdffasdf sadfasdfsadffsfsdf Report End. sdfasdfasdf sdfasfdasdfasdfasdfasdf sadfasdfsdf I need to grab from Report Start... (3 Replies)
Discussion started by: mrskittles99
3 Replies

8. Shell Programming and Scripting

Grabbing data from a secured website

Hello there, I am a beginner in Perl, and I have a challenging project: I have to create a program that checks regularly on an online bank account for new operations, it should then feed a database keeping track of all the money going in and out. Of course the login details of this online... (4 Replies)
Discussion started by: freddie50
4 Replies

9. Shell Programming and Scripting

Reducing the decimal points of numbers (3d coordinates) in a file; how to input data to e.g. Python

I have a file full of coordinates of the form: 37.68899917602539 58.07500076293945 57.79100036621094 The numbers don't always have the same number of decimal points. I need to reduce the decimal points of all the numbers (there are 128 rows of 3 numbers) to 2. I have tried to do this... (2 Replies)
Discussion started by: crunchgargoyle
2 Replies

10. Shell Programming and Scripting

Grabbing text between two lines with shell variables.

I would like to grab complex html text between lines using variables. I am running Debian and using mksh shell. Here is the part of the html that I want to extract from. I would like to extract the words 'to love,' and I would like to use the above and below lines as reference points. ... (3 Replies)
Discussion started by: bedtime
3 Replies
NG_DEVICE(4)						   BSD Kernel Interfaces Manual 					      NG_DEVICE(4)

NAME
ng_device -- device netgraph node type SYNOPSIS
#include <netgraph/ng_device.h> DESCRIPTION
A device node is both a netgraph node and a system device interface. When a device node is created, a new device entry appears which is accessible via the regular file operators such as open(2), close(2), read(2), write(2), etc. The first node is created as /dev/ngd0, all subsequent nodes /dev/ngd1, /dev/ngd2, etc. HOOKS
A device node has a single hook with an arbitrary name. All data coming in over the hook will be presented to the device for read(2). All data coming in from the device entry by write(2) will be forwarded to the hook. CONTROL MESSAGES
The device node supports one non-generic control message: NGM_DEVICE_GET_DEVNAME Returns device name corresponding to a node. SHUTDOWN
This node shuts down upon receipt of a NGM_SHUTDOWN control message, or upon hook disconnection. The associated device entry is removed and becomes available for use by future device nodes. SEE ALSO
netgraph(4), ngctl(8) HISTORY
The device node type was first implemented in FreeBSD 5.0. AUTHORS
Mark Santcroos <marks@ripe.net> Gleb Smirnoff <glebius@FreeBSD.org> BSD
October 19, 2004 BSD
All times are GMT -4. The time now is 01:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy