search & select text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search & select text
# 1  
Old 12-21-2010
Data search & select text

hi people;

from my file:
Code:
...
...
...
101221-18:45:17 192.168.1.1 1.0 PortNodeModel
=========================================================
Object Attribute Value
=========================================================
SectorPort=3 switchport 20
SectorPort=1 switchport 10
SectorPort=2 switchport 0
=========================================================
Total: 3 results
...
...
...
101221-18:45:17 192.168.2.1 1.0 PortNodeModel
=========================================================
Object Attribute Value
=========================================================
RbsLocalCell=S2C1 featureState 20
RbsLocalCell=S4C1 featureState 40
RbsLocalCell=S1C1 featureState 30
RbsLocalCell=S3C1 featureState 90
=========================================================
Total: 4 results
...
...
...
101221-18:45:17 192.168.3.1 1.0 PortNodeModel
=========================================================
Object Attribute Value
=========================================================
=========================================================
Total: 0 results
...
...
...
101221-18:45:17 192.168.4.1 1.0 PortNodeModel
=========================================================
Object Attribute Value
=========================================================
RbsLocalCell=S3C1 maxNumUsers 16
RbsLocalCell=S4C1 maxNumUsers 16
=========================================================
Total: 2 results
...
...
...

i want to get object and values and see:
Code:
192.168.1.1
SectorPort=3 switchport 20
SectorPort=1 switchport 10
SectorPort=2 switchport 0

192.168.2.1
RbsLocalCell=S2C1 featureState 20
RbsLocalCell=S4C1 featureState 40
RbsLocalCell=S1C1 featureState 30
RbsLocalCell=S3C1 featureState 90

192.168.3.1 (since there is 0 result, it is empty)

192.168.4.1
RbsLocalCell=S3C1 maxNumUsers 16
RbsLocalCell=S4C1 maxNumUsers 16


maybe we can search for ====== lines and get values between them. the problem is that, there are 3 ====== lines. we must "get $2" (IP addess) above first ====== line and "get all values" between second and third ====== lines. but i don't know how to search those lines (mod 3).

this is my logical solution, or maybe there is an easy way of it Smilie SmilieSmilieSmilie
# 2  
Old 12-21-2010
something like this:

Code:
 
awk '/^1012/ {print "\n"$2;next } /^Obj/ { s=2;next } /^===============/ {s=s-1;next } s { print }' input_file

This User Gave Thanks to panyam For This Post:
# 3  
Old 12-21-2010
Code:
egrep -ve "^Obj|^====|^Tot" input | sed 's/^.* \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\) .*$/\1/' >output

# 4  
Old 12-21-2010
@ctsgnb, There are some lines '......' in input file that should not be printed.
# 5  
Old 12-21-2010
A bit verbose, but...
Code:
nawk '/^[0-9][0-9]*-/ {print $2;next}/^Object Att/{p=1;next} p && p--{next} /^Total/{print "";p=-1} !p && !/^==/' myFile


Last edited by vgersh99; 12-21-2010 at 12:24 PM..
# 6  
Old 12-21-2010
Quote:
Originally Posted by anurag.singh
@ctsgnb, There are some lines '......' in input file that should not be printed.
...

Code:
egrep -e "Port|RbsL" input | sed 's/^.* \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\) .*$/\1/' >output

... just choose the right pattern you need to grep

Last edited by ctsgnb; 12-21-2010 at 12:27 PM..
This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 12-21-2010
Quote:
Originally Posted by ctsgnb
...

Code:
egrep -e "Port|RbsL" input | sed 's/^.* \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\) .*$/\1/' >output

this doesn't produce the desired output:
Code:
SectorPort=3 switchport 20
SectorPort=1 switchport 10
SectorPort=2 switchport 0
192.168.2.1
192.168.3.1
192.168.4.1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

How to select the shortest path in grep search?

Hi, How can I display only one shortest path (A/B/configure)? $ grep configure file.txt A/B/configure A/B/C/configure A/B/C/D/configure Thank you. (9 Replies)
Discussion started by: hce
9 Replies

3. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

4. Shell Programming and Scripting

Search & Replacing text within a file

Hi all! I'm a newbie and I'm writing a script which will ask a user for data to search. I will then search for this data using grep and displaying this data back to the screen for the user to see. The user will then enter new data to replace the data searched. Now how do I replace this data... (4 Replies)
Discussion started by: macastor
4 Replies

5. UNIX for Dummies Questions & Answers

select nth file & range of files

Two related questions; 1. how do i select the nth file in a directory? 2. how do i select a range of files, say 2nd-5th? I have done the following but I don't think the solution is a neat one. Query 1: select nth file - say 3rd file; #!/bin/bash touch foo1 foo2 foo3 foo4 foo5 foo6... (1 Reply)
Discussion started by: Muhammad Rahiz
1 Replies

6. Shell Programming and Scripting

regex to remove text before&&after comma chars

Hi, all: I have a question about "cleaning up" a huge file with regular expression(s) and sed: The init file goes like this: block1,blah-blah-blah-blah,numseries1,numseries2,numseries3,numseries4 block2,blah-blah-blah-blah-blah,numseries,numseries2,numseries3,numseries4 ...... (3 Replies)
Discussion started by: yomaya
3 Replies

7. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

8. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

9. Shell Programming and Scripting

Advanced Search & Delete Text File

I have a file in which email messages are stored in. Every email is separated by by a ^Z character (Control-Z). I need to extract all emails after the 65,00th one to another file and delete them from the original file. Any suggests on accomplishing this? (2 Replies)
Discussion started by: maxcell
2 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question