How to Get a Particular Line from a File

Get specific line from text file using just shell script

sed:

sed '5!d' file

awk:

awk 'NR==5' file

How to read a specific line using the specific line number from a file in Java?

Unless you have previous knowledge about the lines in the file, there's no way to directly access the 32nd line without reading the 31 previous lines.

That's true for all languages and all modern file systems.

So effectively you'll simply read lines until you've found the 32nd one.

Bash tool to get nth line from a file

head and pipe with tail will be slow for a huge file. I would suggest sed like this:

sed 'NUMq;d' file

Where NUM is the number of the line you want to print; so, for example, sed '10q;d' file to print the 10th line of file.

Explanation:

NUMq will quit immediately when the line number is NUM.

d will delete the line instead of printing it; this is inhibited on the last line because the q causes the rest of the script to be skipped when quitting.

If you have NUM in a variable, you will want to use double quotes instead of single:

sed "${NUM}q;d" file

How to get a particular line from a file

Try one of these two solutions:

file = File.open "file.txt"

#1 solution would eat a lot of RAM
p [*file][n-1]

#2 solution would not
n.times{ file.gets }
p $_

file.close

How to read specific lines from a file (by line number)?

If the file to read is big, and you don't want to read the whole file in memory at once:

fp = open("file")
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break
fp.close()

Note that i == n-1 for the nth line.


In Python 2.6 or later:

with open("file") as fp:
for i, line in enumerate(fp):
if i == 25:
# 26th line
elif i == 29:
# 30th line
elif i > 29:
break

How do I read a specific line on a text file?

as what Barmar said use the file.readlines(). file.readlines makes a list of lines so use an index for the line you want to read. keep in mind that the first line is 0 not 1 so to store the third line of a text document in a variable would be line = file.readlines()[2].
edit: also if what copperfield said is your situation you can do:

def read_line_from_file(file_name, line_number):
with open(file_name, 'r') as fil:
for line_no, line in enumerate(fil):
if line_no == line_number:
file.close()
return line
else:
file.close()
raise ValueError('line %s does not exist in file %s' % (line_number, file_name))

line = read_line_from_file('file.txt', 2)
print(line)
if os.path.isfile('file.txt'):
os.remove('file.txt')

it's a more readable function so you can disassemble it to your liking

Reading a particular line from a text file in Java

here is how you do it:

import java.io.*;

public class Test {
public static void main(String [] args) {

// The name of the file to open.
String fileName = "temp.txt";
int counter = 0;

// This will reference one line at a time
String line = null;
FileReader fileReader = null;

try {
// FileReader reads text files in the default encoding.
fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
counter++;
if(counter == 3 || counter == 8 || counter == 12)
{
// do your code
}
}

}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
finally
{
if(fileReader != null){
// Always close files.
bufferedReader.close();
}
}
}
}

How to get particular line to line from text file and display array list in android

The solution below is find all the lines after '## userot' until the next ##

FileInputStream fileIn = openFileInput("mytextfile.txt");
InputStreamReader inputRead = new InputStreamReader(fileIn);
BufferedReader reader = new BufferedReader(inputRead);

while ((str = reader.readLine()) != null){
if(str.contains("##") && str.contains("userot"){
while ((str = reader.readLine()) != null){
if(str.contains("##"))
break;
else
Log.d("TAG","output: " + str);
}
break;
}
}


Related Topics



Leave a reply



Submit