Show lines between frist and last appearance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Show lines between frist and last appearance
# 1  
Old 11-25-2009
Show lines between frist and last appearance

Dear all,

I want to show all lines between the first and the last appearance of a string in a file. This string is a input parameter of a script. I have some ideas with grep -n but i'm sure that I can get a better solution with SED or AWK

thanks!
# 2  
Old 11-25-2009
As a start, both in awk and sed you can use ranges, written like
Code:
.. /start/,/end/ ..

# 3  
Old 11-25-2009
Sorry but, if you could write the complete command I would understand better... I know how to declare "start" pattern but no "end" pattern, and i supose i must write code to print lines ... thank you!
# 4  
Old 11-25-2009
Code:
> cat  t.lis
1
2
3
3
4
4
5
5
6
6
7
7
8
8
9
10

> sed -n '/^3/,/^8/p' t.lis
3
4
5
6
7
8

start with the line that begins with the character 3 /^3/
end with the line that starts with the chacter 8 /^8/
^ means the start of the line
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting Word Appearance

How do you write a script that counts the number of times a word appears in a file and output it? Original: ID1 SMARCB1;Adil;Jon ID2 Jon;Annie;Mei ID3 Adil;Spaghetti;NBA ID4 Raptors;wethenorth;SMARCB1 ID5 SMARCB1;wethenorth Objective: SMARCB1: 3 Adil: 2 Jon: 2... (5 Replies)
Discussion started by: Lipidil
5 Replies

2. Shell Programming and Scripting

Grep into a file + show following lines

Hi guys, This is probably very easy but I've no idea how to pull this out. Basically, I need to find errors into a very large logfile. When you grep the ID, the output is like this: +- Type: 799911 Code: Ret: 22728954 Mand: X Def: Des: UserDes: SeqNo: 2 +- Type: 799911 Code: Ret:... (5 Replies)
Discussion started by: Arkadia
5 Replies

3. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

4. Shell Programming and Scripting

[Solved] Show lines above and below!

hello team - anyone can help shwoing how to grep a file showing the lines above and below? Thx Bragabio (7 Replies)
Discussion started by: bragabio
7 Replies

5. UNIX for Dummies Questions & Answers

Trying to show lines in INI files until the comment character (#)

I have a working directory on a server with over 100 INI files. For the most part, they are configured the same way. Each line will contain 1 or none variables listed from the first character in the line such as VariableName=0. Unfortunately there are comments everywhere using the... (4 Replies)
Discussion started by: hindesite
4 Replies

6. Shell Programming and Scripting

Show entire lines with diff command

Hi, When I run the diff command using diff -yt file1 file2, I get the output in which original lines are truncated. I tried using -W switch with diff. However, that does not produce exact output as I want. Is it possible to show entire line of file1 and file2 in diff command's output? ... (8 Replies)
Discussion started by: jal_capri
8 Replies

7. Shell Programming and Scripting

Grep string but also it will show the next 5 lines

Hi experts, I want to grep a number 9366109380 from a file but it will also show me the next 5 lines. Below is the example- when i grep 989366109380, i can also see the next 5 lines. Line 1. <fullOperation>MAKE:NUMBER:9366109380:PPAY2;</fullOperation> Line 2.... (10 Replies)
Discussion started by: thepurple
10 Replies

8. Shell Programming and Scripting

grep to show lines only after pattern

When i grep for a pattern the search results comes up with matching lines(some before the pattern and some after)...how can i limit the search so that it shows only the lines after the pattern specified (5 Replies)
Discussion started by: wannalearn
5 Replies

9. UNIX for Dummies Questions & Answers

How can you show lines surrounding a search string?

I would like to be able to grep (or some such thing) a search argument and then display the line plus the preceding 3 lines of the file and the following 3 lines of the file. Any ideas? Thanks in advance! :D (3 Replies)
Discussion started by: robster
3 Replies

10. UNIX for Dummies Questions & Answers

Need ls to show number of lines in each file

Hi, I need a command that would let ls show number of lines in each file rather than file size in KBs. I tried using wc -l as a source of input to ls but I found a problem cutting the file name since wc generates a space delimited list. Any suggestions? Thanks. GmMike. (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question