How to search a text in file and retrieve required lines following it with UNIX command?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to search a text in file and retrieve required lines following it with UNIX command?
# 1  
Old 01-25-2018
How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command.

Eg:

Find the text UNIX in the below file and need to return Test 8 & Test 9

Code:
Test 1
Test 2
Test 3
Test 4
UNIX
Test 5
Test 6
Test 7
Test 8
Test 9

Result can be:

Code:
Test 8
Test 9

Please suggest an answer with examples.

Thanks in advance Smilie


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-25-2018 at 04:35 AM.. Reason: Added CODE tags.
# 2  
Old 01-25-2018
Welcome to the forum.

Any attempts / ideas / thoughts from your side? Please demonstrate why and by which logic the two lines should be selected once the "UNIX" test was found..
# 3  
Old 01-25-2018
I came across below UNIX command, this will return matching text plus the line following it.

Code:
sed -n '/UNIX/,+1p' file.txt >newfile.txt

In the above mentioned example it should return below

Code:
UNIX
Test 5

But this command is not working for me.

The logic I am looking for is to match a file pattern and return the line after the match which is specified by me. (i.e I need to choose which line to return after the text match)

I need some working Unix command for this requirement.

Thanks!!

Last edited by Scott; 01-25-2018 at 05:30 AM.. Reason: Code tags
# 4  
Old 01-25-2018
What error / output do you get?

If you're using Linux, or have access to GNU grep, so can also do this with:
Code:
$ grep -A1 UNIX file
UNIX
Test 5

or with something like:
Code:
$ sed -n '/UNIX/{N;p;}' file
UNIX
Test 5

This User Gave Thanks to Scott For This Post:
# 5  
Old 01-25-2018
And how would you supply the info "after the match which is specified by me". Please give the entire picture, don't have people in here worm it out of you bit by bit.
# 6  
Old 01-25-2018
Hmm. I seem to have missed the first post (was referring to post #3).
# 7  
Old 01-25-2018
I'm sure some of our sed experts could do this with sed, but I find ed with a here-document much easier to deal with when using offsets from a line that matches a pattern. With a POSIX conforming shell (or any shell that uses Bourne shell syntax and supports arithmetic expansions) the following script should do what you want with either ed or ex:
Code:
BRE=UNIX	# Basic Regular Expression to search for.
after=4		# Lines after match where first line to be printed will be found
count=2		# Number of lines to print.
file=file	# Pathname of file to be searched.

ed -s "$file" <<EOF
/$BRE/+$after;.+$((count - 1))p
q
EOF

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk command to retrieve record 23 and 89 from UNIX file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file?... (6 Replies)
Discussion started by: rakeshp
6 Replies

2. UNIX for Beginners Questions & Answers

awk command to retrieve record 23 and 89 from UNIX file

Hi Everyone, I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file? Please let me know what is the awk command for this? Regards Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

5. UNIX for Dummies Questions & Answers

Unix search a string in the text file

File name : Sample.txt Actually i would like to read <schema>Oracle<schema> string from input file and return only once database as my output. Please advise me. Moved to appropriate forum. (1 Reply)
Discussion started by: balajikalai
1 Replies

6. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

7. Shell Programming and Scripting

Getting required fields from a test file in required fromat in unix

My data is something like shown below. date1 date2 aaa bbbb ccccc date3 date4 dddd eeeeeee ffffffffff ggggg hh I want the output like this date1date2 aaa eeeeee I serached in the forum but didn't find the exact matching solution. Please help. (7 Replies)
Discussion started by: rdhanek
7 Replies

8. Shell Programming and Scripting

Getting required fields from a text file in UNIX

My data is something like as shown below. Out of this i want the details of alarms (ex: 1947147711,1947147081......) and the fields( ex :sw=tacmwafabb9:shelf=1:slot=5-2:pport=2) Once i have these details separated, i want the count of these excluding the duplicates. What is the best possible way... (7 Replies)
Discussion started by: rdhanek
7 Replies

9. Shell Programming and Scripting

how to retrieve required specific info from the file

Hi I have a file which consists of a number in the square brackets, followed by the blank line, then several lines which describe this number. This pattern is repeated several thousands time. The number in the brackets and the decription of it is unique. For example: ASRVSERV=1241GD;... (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies
Login or Register to Ask a Question