Sponsored Content
Top Forums Shell Programming and Scripting Grep: show only first match per line? Post 303000849 by RudiC on Friday 21st of July 2017 01:12:49 AM
Old 07-21-2017
How about
Code:
awk '
NR == FNR       {SRCH = SRCH DL $0
                 DL = "|"
                 next
                }
match ($0, SRCH){print substr ($0, RSTART, RLENGTH)
                }
' patterns.txt file.txt

This User Gave Thanks to RudiC For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

2. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

3. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

4. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

5. UNIX for Dummies Questions & Answers

Greping next line after grep match

Hi All I have a file with tha same line multiple times and its easy to grep out those lines using grep "pattern" filealthough I need to know exactly what the next line after those lines are Can anyone please shed some light on this on how i can simultaneously grep the pattern and the next line... (9 Replies)
Discussion started by: pawannoel
9 Replies

6. UNIX for Dummies Questions & Answers

Grep help (match two strings one line)

Hello, Here I have some grep command which is not working correctly: cat file1.txt: apples Date: Sun, 24 Feb 2013 8:14:06 -0800 peaches melons cherry sky cloud green purple yellow cat file2.txt: apples Date peaches melons 0800 cherry sky cloud green purple black (2 Replies)
Discussion started by: holyearth
2 Replies

7. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

8. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

9. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

10. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies
MB_REGEX_SET_OPTIONS(3) 						 1						   MB_REGEX_SET_OPTIONS(3)

mb_regex_set_options - Set/Get the default options for mbregex functions

SYNOPSIS
string mb_regex_set_options ([string $options = mb_regex_set_options()]) DESCRIPTION
Sets the default options described by $options for multibyte regex functions. PARAMETERS
o $options - The options to set. This is a string where each character is an option. To set a mode, the mode character must be the last one set, however there can only be set one mode but multiple options. Regex options +-------+-------------------------------+---+---+ |Option | | | | | | | | | | | Meaning | | | | | | | | +-------+-------------------------------+---+---+ | i | | | | | | | | | | | Ambiguity match on | | | | | | | | | x | | | | | | | | | | | Enables extended pattern form | | | | | | | | | m | | | | | | | | | | | | | | | | '.' matches with newlines | | | | | | | | | s | | | | | | | | | | | | | | | | '^' -> 'A', '$' -> '' | | | | | | | | | p | | | | | | | | | | | Same as both the m and s | | | | | options | | | | | | | | | l | | | | | | | | | | | Finds longest matches | | | | | | | | | n | | | | | | | | | | | Ignores empty matches | | | | | | | | | e | | | | | | | | | | | eval(3) resulting code | | | | | | | | +-------+-------------------------------+---+---+ Regex syntax modes +-----+----------------------------+---+---+ |Mode | | | | | | | | | | | Meaning | | | | | | | | +-----+----------------------------+---+---+ | j | | | | | | | | | | | Java (Sun java.util.regex) | | | | | | | | | u | | | | | | | | | | | GNU regex | | | | | | | | | g | | | | | | | | | | | grep | | | | | | | | | c | | | | | | | | | | | Emacs | | | | | | | | | r | | | | | | | | | | | Ruby | | | | | | | | | z | | | | | | | | | | | Perl | | | | | | | | | b | | | | | | | | | | | POSIX Basic regex | | | | | | | | | d | | | | | | | | | | | POSIX Extended regex | | | | | | | | +-----+----------------------------+---+---+ RETURN VALUES
The previous options. If $options is omitted, it returns the string that describes the current options. SEE ALSO
mb_split(3), mb_ereg(3), mb_eregi(3). PHP Documentation Group MB_REGEX_SET_OPTIONS(3)
All times are GMT -4. The time now is 05:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy