Sponsored Content
Top Forums Shell Programming and Scripting Search for a pattern in a file and print previous lines from a particular point Post 302301788 by umaislearning on Saturday 28th of March 2009 10:20:42 AM
Old 03-28-2009
Quote:
Originally Posted by Ikon
Something like this??
Code:
root@jeepanywhere /usr/src # cat test.log
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
root@jeepanywhere /usr/src # sed -n '/^2/,/^4/{/LABEL$/!p}' test.log
2
3
4
2
3
4
2
3
4

I am not able to understand this command very clearly, but this is wat I want, please tell me if that sed command will give me the below output:

suppose I have a file like the one below:

start of file-----
***********
1
2
3
4
5
========
***********
6
7
8
9
========
end of file-----
Now for eg I need to search for 8, then search for the first occurance of the pattern ***** thats jus few lines(no. of lines not fixed) above 8...then from there I need to print till end of file....
so my output needs to be something like this:
***********
6
7
8
9
============
end of file-----

Thanks in advance...
 

9 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. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

3. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

4. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

5. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. 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

8. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

9. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies
GLOBAL(1)						      General Commands Manual							 GLOBAL(1)

NAME
global - print the locations of specified object. SYNOPSIS
global [-aGilnqrstTvx][-e] pattern global -c[qrsv] prefix global -f[anqrstvx] files global -g[aGilnoOqtvx][-e] pattern global -I[ailnqtvx][-e] pattern global -P[aGilnoOqtvx][-e] pattern global -p[qrv] global -u[qv] DESCRIPTION
Global find the locations of specified object in C, C++, Yacc, Java, PHP and Assembly source files. Global can treat a source tree, that is, a directory that has sub-directories and source files as a project. You can get the relative path of objects from anywhere within the project. Global can locate not only object definitions but also object references and other symbols. In advance of using this command, you must execute gtags(1) at the root directory of the project to make tag files. Then you can execute this command at anywhere in the project. COMMANDS
The following commands are available: <no command> pattern Print object which match to the pattern. Extended regular expressions which are the same as those accepted by egrep(1) are avail- able. -c, --completion [prefix] Print the candidates of object names which start with the specified prefix. Prefix is not specified, print all object names. -f, --file files Print all tags in the files. This option implies the -x option. -g, --grep pattern Print all lines which match to the pattern. --help Show help. -I, --idutils pattern Print all lines which match to the pattern. This function use idutils(1) as a search engine. To use this command, you need to install idutils(1) in your system and you must execute gtags(1) with the -I option. -P, --path [pattern] Print the paths which match to the pattern. If no pattern specified, print all paths in the project. -p, --print-dbpath Print the location of 'GTAGS'. -u, --update Locate tag files and update them incrementally. --version Show version number. OPTIONS
The following options are available: -a, --absolute Print absolute path name. By default, print relative path name. --from-here context Decide tag type by the context. The context must be 'lineno:path'. If this option is specified then the -s and -r are ignored. Regular expression is not allowed in the pattern. This option is assumes use in conversational environments such as editors and IDEs. -e, --regexp pattern Use pattern as the pattern; useful to protect patterns beginning with '-'. -G, --basic-regexp Interpret pattern as a basic regular expression. The default is extended regular expression. -i, --ignore-case ignore case distinctions in pattern. -l, --local Print just objects which exist under the current directory. -n, --nofilter Suppress sort filter and path conversion filter. -O, --only-other Search pattern only in other than source files like 'README'. This option is valid only with -g or -P command. This option over- ride the -o option. -o, --other Search pattern in not only source files but also other files like 'README'. This option is valid only with -g or -P command. -q, --quiet Quiet mode. -r, --reference, --rootdir Print the locations of object references. By default, print object definitions. With the -p option, print the root directory of source tree. --result format format may be 'path', `ctags', `ctags-x', `grep' or 'cscope'. The --result=ctags and --result=ctags-x are equivalent to the -t and -x respectively. The --result option is given to priority more than the -t and -x option. -s, --symbol Print the locations of specified symbol other than definitions. -T, --through Go through all the tag files listed in GTAGSLIBPATH. By default, stop searching when tag is found. This option is ignored when either -s, -r or -l option is specified. -t, --tags Print with standard ctags format. -v, --verbose Verbose mode. -x, --cxref In addition to the default output, produce the line number and the line contents. EXAMPLES
$ ls -F Makefile src/ lib/ $ gtags $ global main src/main.c $ global -x main main 10 src/main.c main (argc, argv) { $ global -x '^[sg]et' set_num 20 lib/util.c set_num(values) get_num 30 lib/util.c get_num() { $ global -rx '^[sg]et' set_num 113 src/op.c set_num(32); set_num 225 src/opop.c if (set_num(0) > 0) { get_num 90 src/op.c while (get_num() > 0) { $ cd lib $ global -rx '^[sg]et' set_num 113 ../src/op.c set_num(32); set_num 225 ../src/opop.c if (set_num(0) > 0) { get_num 90 ../src/op.c while (get_num() > 0) { $ global strlen $ (cd /usr/src/sys; gtags) $ export GTAGSLIBPATH=/usr/src/sys $ global strlen ../../../usr/src/sys/libkern/strlen.c $ (cd /usr/src/lib; gtags) $ GTAGSLIBPATH=/usr/src/lib:/usr/src/sys $ global strlen ../../../usr/src/lib/libc/string/strlen.c FILES
'GTAGS' Tag file for object definitions. 'GRTAGS' Tag file for object references. 'GSYMS' Tag file for other symbols. 'GPATH' Tag file for path of source files. 'GTAGSROOT' If environment variable GTAGSROOT is not set and 'GTAGSROOT' exist in the same directory with 'GTAGS' then use the value as GTAGS- ROOT. '/etc/gtags.conf', '$HOME/.globalrc' Configuration file. ENVIRONMENT
The following environment variables affect the execution of global: GTAGSROOT The directory which is the root of source code. GTAGSDBPATH The directory on which gtags database exist. This value is ignored when GTAGSROOT is not defined. GTAGSLIBPATH If this variable is set, it is used as the path to search for library functions. If the specified function is not found in the source project, global also search in these paths. GTAGSLABEL If this variable is set, its value is used as the label of configuration file. The default is default. MAKEOBJDIRPREFIX If this variable is set, '$MAKEOBJDIRPREFIX<current directory>' is used as the candidate directory for tag files. CONFIGURATION
The following configuration variables affect the execution of global: icase_path(boolean) Ignore case distinctions in the pattern. DIAGNOSTICS
Global exits with a non 0 value if an error occurred, 0 otherwise. SEE ALSO
gtags-parser(1), gtags(1), htags(1), less(1). GNU GLOBAL source code tag system (http://www.gnu.org/software/global/). AUTHOR
Tama Communications Corporation. HISTORY
The global command appeared in FreeBSD 2.2.2. GNU Project March 2008 GLOBAL(1)
All times are GMT -4. The time now is 06:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy