Help needed to extracting lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed to extracting lines
# 1  
Old 01-06-2014
Help needed to extracting lines

Hello,

I need help in extracting lines that meets following criteria

Grep for a particular word and return line before it, the line that has that word, and line after that

Code:
:::::::::::::::::::::::::
Subject
Hi there
My name is Jak
My last name is XYZ
Home Address
::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::

If i grep for Jak the output should give

Code:
Hi there
My name is Jak
My last name is XYZ

# 2  
Old 01-06-2014
Code:
grep -A1 -B1 Jak file

# 3  
Old 01-07-2014
Yes you can try this

grep -A (num of lines after) -B (num of lines before) Jak file



Cheers,
Adi
# 4  
Old 01-07-2014
With Sed
Code:
michaelf>uname
SunOS
michaelf>sed -n 'H
> /Jak/{
> g
> s/.*\n\(.*\n.*\)/\1/
> N
> p
> }' infile
Hi there
My name is Jak
My last name is XYZ
michaelf>

# 5  
Old 01-07-2014
Thanks everyone for prompt help!
# 6  
Old 01-08-2014
Code:
grep -C1 Jak file

It will give above yes and below lines
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting the two lines where the first line is matched

Hi, If I have a file of something like @hg19_gold_AL122127.6-131160 GCTTCATCATGCATGGATAGGCTGGCGCCTTTCCTGAGGCCATATGCCGATGGATATG @hg19_gold_AL122127.6-131159 CTTTAATATTTCCGCCACCATCCTGAGTGAATCCCAGCAAGGACAGTCTTTGGGGATT @hg19_gold_AL122127.6-131158... (4 Replies)
Discussion started by: jyu429
4 Replies

2. Shell Programming and Scripting

extracting lines from a file with similar first name

consider i have two files cat onlyviews1.sql CREATE VIEW V11 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V22 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V33 AS (10 Replies)
Discussion started by: vivek d r
10 Replies

3. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

4. UNIX for Advanced & Expert Users

Extracting Lines in a file

Hi, We have a file where we need to extract the lines of a file based on the first three characters of every line in that file. Eg: RAM hill station SAM student RAM rose SAM apple RAM india SAM australia RAM happy RAM heaven SAM smile RAM glow So, all the lines that start... (5 Replies)
Discussion started by: pyaranoid
5 Replies

5. Shell Programming and Scripting

Extracting lines by using awk

Hello, I have a file including some lines as follows person (1): a d t person (2): f h j person (3): z x v . . . (8 Replies)
Discussion started by: rpf
8 Replies

6. UNIX for Dummies Questions & Answers

Extracting m lines after n lines after match

Hi All, I would like to extract from a text file m lines skipping n lines after a string occurrency. Is it possible with grep? e.g. qqq ww eee rrr ttt yyy uuu I want to print 2 lines skipping 1 line after the string 'ww' result would be rrr ttt (2 Replies)
Discussion started by: f_o_555
2 Replies

7. Shell Programming and Scripting

Help needed in extracting text present between two headers in .txt file

Hi All, Please help me out in fllowing problem. I have text file which contains the data in following format. Contents of file.txt are setregid02 Test that setregid() fails and sets the proper errno values when a non-root user attemps to change the real or effective... (2 Replies)
Discussion started by: varshit
2 Replies

8. Shell Programming and Scripting

Extracting text out of specific lines

Hi, I have a file like LAHORE 2009-04-16 16:04:19 THU S5830 FAULT MESSAGE SUPPRESS STATUS LOC : ASP00 STS : SUPPRESSING CONTINUE INF : F6201 TRUNK. DATA FAULT REPORT COMPLETED LAHORE 2009-04-16 16:04:20 THU S8400 ISUP SIGNALLING TRACE -... (3 Replies)
Discussion started by: krabu
3 Replies

9. UNIX for Dummies Questions & Answers

extracting lines from a file

i want to extract lines 5 and 7 from a txt file which contains nearly 20 entries how to do it also i want to check whether the 42nd character is 'S' in that line suggestions welcome (4 Replies)
Discussion started by: trichyselva
4 Replies

10. Shell Programming and Scripting

extracting lines from a file

Hi all, I need to extract some lines from a file based on a condition. For ex: My file will contain 50 lines and i need to extract line which has "File" in it and then the line which has "date" in it. Which command will be the most efficient way to do it. I have tried two ways 1.... (1 Reply)
Discussion started by: pradeepthanraj
1 Replies
Login or Register to Ask a Question