Dynamically accept search pattern and display lines based on it | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Dynamically accept search pattern and display lines based on it

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2013
nsuresh316 nsuresh316 is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 21 June 2013, 6:35 AM EDT
Posts: 24
Thanks: 11
Thanked 0 Times in 0 Posts
Dynamically accept search pattern and display lines based on it

I have a output file which contains n number of document.Each document has n number of segments and identified using below points

The starting segment is ISA and Ending segment is IEA
Each document has unique number and it will be passed in REF*D9 segment

Each line in sample file is called segment and each segment is group of field delimited with (*)

Since i cant paste the entire contents i have attached the sample file.

Refer the sample file for exact file structure.

If i need to display particular segment from large sample file

Code:
For eg N3 segment which is under NM1*85 segments for following documents alone

12000000010
12000000011
12000000012
12000000013
12000000014
12000000015
12000000016
12000000017

I used the following commands


Code:
1.awk 'a-->0;/NM1\*85/{a=1}/REF\*D9/' file > file1
2.nawk '{s=s?s"\t"$0:$0}/REF/{print s;s=""}' file1|sed -n '/12000000010/,/12000000017/p'

if i need N4 segment which is under NM1*85 segment for same set of documents then i use

Code:
3.awk 'a-->0;/NM1\*85/{a=2}/REF\*D9/' file|egrep -v "N3" > file2
4.nawk '{s=s?s"\t"$0:$0}/REF/{print s;s=""}' file2|sed -n '/12000000010/,/12000000017/p'

so for each and every segment i need to type the command again and store the output in seperate file


Code:
So I need something user interactive

i.e it should prompt 
for search pattern and no of lines to be displayed after that pattern as i mentioned in 1st command
for segment to be omitted as i mentioned in 3rd command(egrep)
and also for start and end document no. as i mentioned in 4rd command(sed)
all these validation need to be done in tmp file or something
and as soon i provided start and end document no it should display the output

I am novice to unix i got this idea if anybody can come up with better logic that will handle my requirement
it would be appreciable


Code:
OS - SunOS viper 5.9 Generic_117171-07 sun4u sparc SUNW,Sun-Fire-480R
Shell - csh

Attached Files
File Type: txt file.txt (520.9 KB, 19 views)
Sponsored Links
    #2  
Old 03-07-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 15 October 2014, 5:08 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,455
Thanks: 8
Thanked 546 Times in 524 Posts
A shell loop can ask for the pattern, capture matching lines in a temp file, count the lines at the same time, and if any lines, turn you over to vi, pg, less in the temp file to view and play in the subset. When you exit the viewer, it asks you if you want to view again, and if not, loops. You can make any copies you want from inside the viewer.

Now, only a select few actually prefer the csh. The bash and ksh93 are much nicer and there are more experts to help.

PS: I use the ksh/bash 'set -o vi' option to do command recall, so I do not type commands over and over. I never even leave my home dir except inside (...) so I return automatically. It makes the history more valuable. I also save the history file occasionally where my tools can find it, and set a huge HISTSIZE of 32767. I consider one of the best CUI's.

Last edited by DGPickett; 03-07-2013 at 01:46 PM..
Sponsored Links
    #3  
Old 03-08-2013
nsuresh316 nsuresh316 is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 21 June 2013, 6:35 AM EDT
Posts: 24
Thanks: 11
Thanked 0 Times in 0 Posts

Code:
ok i will be happy if i get the solution based on any shell

    #4  
Old 03-08-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 15 October 2014, 5:08 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,455
Thanks: 8
Thanked 546 Times in 524 Posts
In bash, something like this? File names go on command line.
Code:
#!/bin/bash
 
ans=y
 
while [[ "$ans" == '[Yy]*' ]]
 do
  echo "Enter pattern: \c"
  read ptn
  echo 'Searching . . . \c'
  lc=$( grep -E "$ptn" "$@" | tee /tmp/patgrep.$$ | wc -l )
  echo "$lc lines found."
 
  if [ $lc != 0 ]
   then
    sleep 6
    vi /tmp/patgrep.$$
   fi
 
  echo 'Another (y/n)? \c'
  read ans
 done

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need one liner to search pattern and print everything expect 6 lines from where pattern match made chidori Shell Programming and Scripting 8 03-15-2012 11:46 AM
Extracting few lines from a file based on identifiers dynamically vivek d r Shell Programming and Scripting 14 11-28-2011 04:18 AM
Search and replace - pattern-based Roevhat Shell Programming and Scripting 1 10-10-2011 05:48 AM
Print a pattern between the xml tags based on a search pattern oky Shell Programming and Scripting 13 03-21-2011 01:03 PM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 02:41 PM



All times are GMT -4. The time now is 10:16 PM.