Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Dynamically accept search pattern and display lines based on it Post 302777789 by DGPickett on Friday 8th of March 2013 11:26:49 AM
Old 03-08-2013
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

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies

2. Shell Programming and Scripting

Pattern matching in file and then display 10 lines above every time

hiii, i have to write a shell script like this---- i have a huge log file name abc.log .i have to search for a pattern name "pattern",it may occur 1000 times in the log file,every time it finds the pattern it should display the 10 lines above the pattern. I appericiate your help. (30 Replies)
Discussion started by: namishtiwari
30 Replies

3. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

4. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

5. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. UNIX for Dummies Questions & Answers

Updating value based on search pattern

I have a file with following data <Field FieldName="CHCFA21_01_01" FieldType="Text"> <Output CapturedValue=""> <DataSource Name="" Value="" /> </Output> </Field> <Field FieldName="CHCFA21_01_02" FieldType="Date"> <Output CapturedValue=""> ... (1 Reply)
Discussion started by: nsuresh316
1 Replies

8. Shell Programming and Scripting

Display 2 lines before and after a particular pattern

Hi team, Is it possible to display 2 lines after a particular pattern in a shell script. For example in a file which has the below contents. Mummy Daddy Son Daughter Children Aunty Uncle Grandma Grandpa Son Father Mother Brother-in-law I want to display 2 lines before and after... (1 Reply)
Discussion started by: balamv
1 Replies

9. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

10. Shell Programming and Scripting

Grep pattern and display all lines below

Hi I need to grep for a patter and display all lines below the pattern. For ex: say my file has the below lines file1 file2 file3 file4 file5 I NEED to grep for patter file3 and display all lines below the pattern. do we have an option to get this data. Let me know if you require... (5 Replies)
Discussion started by: venkidhadha
5 Replies
rpmatch(3)						     Library Functions Manual							rpmatch(3)

NAME
rpmatch - Determines whether a response is affirmative or negative LIBRARY
Standard C Library (libc.a) SYNOPSIS
#include <stdlib.h> int rpmatch( const char *response); PARAMETERS
User input entered in response to a question that requires an affirmative or negative answer. DESCRIPTION
The rpmatch() function determines whether the string value of the response parameter matches the affirmative or negative response expres- sion as specified by the LC_MESSAGES category in the program's current locale. Both response expressions, defined in the locale, may be extended regular expressions. A possible value of the affirmative expression, yesexpr, for a English-language locale is "^([yY]|[yY][eE][sS])". This expression will match any value of the response parameter that has consists of the letter Y (in uppercase or lowercase) or the letters YES (in any mixture of uppercase and lowercase letters). EXAMPLES
The following example requests a response from the user and uses the rpmatch() function to determine if the response is affirmative or neg- ative. #include <stdlib.h> #include <stdio.h> #include <locale.h> #include <string.h> #define SLENGTH 80 main() { char str[SLENGTH], *eol; int ans; (void)setlocale(LC_ALL, ""); printf("Do you want to perform this operation: "); fgets(str, SLENGTH, stdin); if ((eol = strchr(str, ' ')) != NULL) *eol = ''; /* Replace newline with null */ else return; /* Line entered too long */ ans = rpmatch(str); if (ans == 1) printf("You responded affirmatively "); else if (ans == 0) printf("You responded negatively "); else printf("Your answer did not match "); } RETURN VALUES
A value of 1 is returned if the string value of the response parameter is matched by the affirmative expression; a value of 0 (zero) is returned if the string value of the response parameter is matched by the negative expression. If neither expression matches the string value of the response parameter, a value of -1 is returned. RELATED INFORMATION
Commands: grep(1). Functions: regcomp(3), regexec(3), setlocale(3). Files: locale(4). delim off rpmatch(3)
All times are GMT -4. The time now is 12:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy