Sponsored Content
Top Forums Shell Programming and Scripting awk or sed for finding closest pattern to a line number Post 302245704 by alirezan on Friday 10th of October 2008 06:10:44 PM
Old 10-10-2008
awk or sed for finding closest pattern to a line number

hi guys,

I want to do pattern matching with awk or sed but I don't know how. here's what I want:

I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk between "---" and that line number. But here's the catch. the pattern "---" can be between 1 and 20 lines above that line number and I don't know how many lines above ... Also, another catch is that, "---" can happen multiple times in the file but I want the "CLOSEST" to the line number. Here's an example:

Here's the file:

Quote:
---
aaa
bbb
ccc
ddd

---
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh

---
jjj
dsf
qqq
www
eee
I found the pattern say qqq to be on line 20. I want to find the first --- before pattern qqq (line 20) and I want to print the entire chunk from --- till the qqq pattern printed out.

Can someone help me out here please?
Thanks
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

2. Shell Programming and Scripting

Include Line Before Pattern Using Sed / Awk

Hi, I have a sql file that runs something like this vi Test.sql REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM PUBLIC; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM DEMO_READ; REVOKE SELECT ON DEMO_USER.DEMO_NOMINEE_TEST FROM DEMO_READ; REVOKE EXECUTE ON DEMO_USER.SQC_SAMP FROM... (3 Replies)
Discussion started by: rajan_san
3 Replies

3. UNIX for Advanced & Expert Users

sed match closest/nearest pattern

All i am struggling to raplace some text in a line between two (closest) patterns , line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin" i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text. i tried teh... (1 Reply)
Discussion started by: sudheer1984
1 Replies

4. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

5. Shell Programming and Scripting

Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file cat fileName.txt (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10... (13 Replies)
Discussion started by: vivek d r
13 Replies

6. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

7. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

9. Shell Programming and Scripting

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

10. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies
PCREGREP(1)						      General Commands Manual						       PCREGREP(1)

NAME
pcregrep - a grep with Perl-compatible regular expressions. SYNOPSIS
pcregrep [-Vcfhilnrsvx] pattern [file] ... DESCRIPTION
pcregrep searches files for character patterns, in the same way as other grep commands do, but it uses the PCRE regular expression library to support patterns that are compatible with the regular expressions of Perl 5. See pcre(3) for a full description of syntax and semantics. If no files are specified, pcregrep reads the standard input. By default, each line that matches the pattern is copied to the standard out- put, and if there is more than one file, the file name is printed before each line of output. However, there are options that can change how pcregrep behaves. Lines are limited to BUFSIZ characters. BUFSIZ is defined in <stdio.h>. The newline character is removed from the end of each line before it is matched against the pattern. OPTIONS
-V Write the version number of the PCRE library being used to the standard error stream. -c Do not print individual lines; instead just print a count of the number of lines that would otherwise have been printed. If sev- eral files are given, a count is printed for each of them. -ffilename Read patterns from the file, one per line, and match all patterns against each line. There is a maximum of 100 patterns. Trailing white space is removed, and blank lines are ignored. An empty file contains no patterns and therefore matches nothing. -h Suppress printing of filenames when searching multiple files. -i Ignore upper/lower case distinctions during comparisons. -l Instead of printing lines from the files, just print the names of the files containing lines that would have been printed. Each file name is printed once, on a separate line. -n Precede each line by its line number in the file. -r If any file is a directory, recursively scan the files it contains. Without -r a directory is scanned as a normal file. -s Work silently, that is, display nothing except error messages. The exit status indicates whether any matches were found. -v Invert the sense of the match, so that lines which do not match the pattern are now the ones that are found. -x Force the pattern to be anchored (it must start matching at the beginning of the line) and in addition, require it to match the entire line. This is equivalent to having ^ and $ characters at the start and end of each alternative branch in the regular expression. SEE ALSO
pcre(3), Perl 5 documentation DIAGNOSTICS
Exit status is 0 if any matches were found, 1 if no matches were found, and 2 for syntax errors or inacessible files (even if matches were found). AUTHOR
Philip Hazel <ph10@cam.ac.uk> Last updated: 15 August 2001 Copyright (c) 1997-2001 University of Cambridge. PCREGREP(1)
All times are GMT -4. The time now is 02:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy