How to print the number of lines from a file, the starting string should be passed`


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the number of lines from a file, the starting string should be passed`
# 1  
Old 05-13-2009
MySQL How to print the number of lines from a file, the starting string should be passed`

Hi ,

I have file, which has the below content:

line 100
a
b
c
d
line300
a
s
d
f
s
line200
a
s
d
a

I want to display the content starts from line300 and i need to copy the 10 sequent lines.

One more requirement is to display the lines starting from line300 and upto the line200

Note:
I have tried a shell script with 'sed', but i couldn't get desiered output. please help me..


Thankyou all in Advance...
# 2  
Old 05-13-2009
I want to display the content starts from line300 and i need to copy the 10 sequent lines.

Code:
awk '/line300/,/ / { print;}' awk_ip | head -10

One more requirement is to display the lines starting from line300 and upto the line200

Code:
 
awk '/line300/,/line200/  { print;}' file_name

# 3  
Old 05-13-2009
Code:
awk -v n=10 '/300/{for(i=1;i<=n;i++){print;getline};exit}' file

Code:
sed -n '/line300/,/line200/{p}' file


cheers,
Devaraj Takhellambam
# 4  
Old 05-13-2009
Hammer & Screwdriver Not pretty, but done in bash

Your first request...

Code:
> cat file20
line 100
a
b
c
d
line300
a
s
d
f
s
line200
a
s
d
a

> tail +`cat -n file20 | grep "line300" | awk '{print $1}'` file20 | head -10
line300
a
s
d
f
s
line200
a
s
d

The previous solutions are NICER, but this uses some commands in unusual ways, so I thought it would be interesting (and educational) to see them used in this manner.
Explained:
I put a line number with each of your lines
grep for the requested text
determine its line number
start my display at that line number
and continue for ten lines
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

4. Shell Programming and Scripting

How to print the lines in a file for the given string?

Hi, I have a file with contents test id text day test sah dh dhs yeay fg jsh jsjk my need: I give a string as a input, it check the file and display the lines with the given string e.g input : test output: test id text day test sah dh dhs (1 Reply)
Discussion started by: nanthagopal
1 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

7. Emergency UNIX and Linux Support

Urgent help pls.how to extract two lines having same starting number

Hi , I have a huge file like this =245 this is testing =035 abc123 =245 this is testing1 =035 abc124 =245 this is testing2 =035 abc125 =035 abc126 =245 this is testing3 here i have to pull out those lines having two =035 instead of alternative 035 and 245 i.e extract... (18 Replies)
Discussion started by: umapearl
18 Replies

8. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

9. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

10. Shell Programming and Scripting

Need help to print lines contains particular string format in a file

Hi, I want to print the lines in a file that matches particular string format using shell scripting. (4 Replies)
Discussion started by: sudhakaryadav
4 Replies
Login or Register to Ask a Question