Sponsored Content
Top Forums Shell Programming and Scripting How to print the number of lines from a file, the starting string should be passed` Post 302315908 by joeyg on Wednesday 13th of May 2009 01:24:35 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
read(3pm)						User Contributed Perl Documentation						 read(3pm)

NAME
OpaL::read - Perl extension for reading files and commands. SYNOPSIS
use OpaL::read qw(functions); No functions or variables are exported automaticly so you have to specify them here. DESCRIPTION
OpaL::read is used for reading files and commands. All functions are autoloaded so they will not be loaded into memory if you have not used them before. FUNCTIONS
readfile Reads the content of the specified file and returns a list with the all lines. USAGE: @foo = "readfile"($filename); readfileline Reads the first line of the specified file and returns a scalar with that line. USAGE: $foo = "readfileline"($filename); readscalarfile Reads the content of the specified file and returns a scalar with the entire content. USAGE: $foo = "readscalarfile"("filename"); readcommand Reads the content from the output of the specified command and returns a list with the all lines. USAGE: @foo = "readcommand"("command"); readcommandline Reads the first line from output of the specified command and returns a scalar with that line. USAGE: $foo = "readcommandline"("command)"; readscalarfile Reads the content for the output of the specified file and returns a scalar with the entire content. USAGE: $foo = "readscalarcommand"($command); AUTHOR
Ola Lundqvist <ola@inguza.com> SEE ALSO
perl(1). perl v5.10.1 2012-08-21 read(3pm)
All times are GMT -4. The time now is 12:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy