Help with "grep", need to display several lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with "grep", need to display several lines
# 1  
Old 04-02-2008
Help with "grep", need to display several lines

I have a file from which I need to collect lines to another file. I know how to use 'grep' for this, but I don't know how to do when I wan't several lines after the search word, and the amount of lines I need varies from case to case.

The file I search in looks something like this:

>8534734
ddkdfdmdmgdgfdgdgdgd
dgdgafasdffsfsfdsfdgtfhj
fdhlffff
>586993
fglslmsvmsmgfsdggdgsdqqg
fhdfdd

...and so on.

I have the numbers (my search 'input'), and I need the numbers AND the rows with letters following. Can I somehow tell the computer to give me everthing from the number to the next '>'?
I have figured how to get the thing that I search for, and a certain amount of rows after that (by using grep -A number 'word' file), but thats not really what I wan't....
# 2  
Old 04-02-2008
Here's an example how you can get the first part:

Code:
awk -v num=">8534734" '
$0 ~ num{p++;next}
p && />.*/{p=0}
p{print}
' file

Regards
# 3  
Old 04-02-2008
Sorry, but I don't understand how to use that code... I just wrote it, but it didn't worked. Maybe I shuold say that I use Putty, since I have a windows computer. And I don't know anything about programing.
# 4  
Old 04-02-2008
you might be happier with something like this ...

awk -v num=">8534734" '
BEGIN {
FS="\n"
RS=""
}
{
print $1 ", " $2 ", " $3
}
p{print}
' test

8534734 is your search term and replace test with the name of your file
# 5  
Old 04-03-2008
I just get syntax error when I'm trying. I might be writing the code wrong. I can't change lines as you do, so I have to write all the lines in one, and then I'm not sure if I should put a "space" where you change line, or what I should do. If I try to change line, Putty takes that as a "run".
# 6  
Old 04-03-2008
If I understand correctly that you want additional lines after the matched line, do "man grep" and read about the -A option. I think it does what you want.

ShawnMilo
# 7  
Old 04-03-2008
I have tried o use -A, and sure, it works, but not as I want. I don't always want the same number of lines, so if I use -A I still have to look throught everything manually and delete lines that I don't want.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. UNIX for Dummies Questions & Answers

Grep : Filter/Move All The Lines Containing Not More Than One "X" Character Into A Text File

Hi All It's me again with another huge txt files. :confused: What I have: - I have 33 huge txt files in a folder. - I have thousands of line in this txt file which contain many the letter "x" in them. - Some of them have more than one "x" character in the line. What I want to achieve:... (8 Replies)
Discussion started by: Nexeu
8 Replies

3. Shell Programming and Scripting

Grep all lines with the string "TNS-" but skip those with "TNS-12514"

Platform: Oracle Linux 6.3 From a log file, I want to grep all lines with the pattern "TNS-" but I want to skip those with the pattern "TNS-12514" . How can I do this ? (3 Replies)
Discussion started by: John K
3 Replies

4. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

5. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

Finding a "word" through grep but display line above?

Hi guys. I am trying to perform a search using grep. I get my grep to work, but need to "awk" a Process Number that is 2 lines above... Example: I run a query on my TSM server for Processes that are "Waiting" for something...it returns this: Process Number: 32,881 Process... (14 Replies)
Discussion started by: Stephan
14 Replies

7. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

8. UNIX for Dummies Questions & Answers

Display all the lines that contain "Straw" followed somewhere in the line by Hat?

how do i use this in a grep pattern, the output is inventory (3 Replies)
Discussion started by: 3dd1e
3 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question