在不使用Perl中的正则表达式的情况下,您可以使用其他编程语言或命令行工具来找到文件中的特定行。以下是一些建议:
with open('file.txt', 'r') as file:
for line in file:
if '特定行' in line:
print(line)
grep '特定行' file.txt
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FindLine {
public static void main(String[] args) {
String fileName = "file.txt";
String targetLine = "特定行";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(targetLine)) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
using System;
using System.IO;
class FindLine
{
static void Main()
{
string fileName = "file.txt";
string targetLine = "特定行";
using (StreamReader sr = new StreamReader(fileName))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(targetLine))
{
Console.WriteLine(line);
}
}
}
}
}
const fs = require('fs');
const targetLine = '特定行';
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const lines = data.split('\n');
lines.forEach((line) => {
if (line.includes(targetLine)) {
console.log(line);
}
});
});
这些方法都可以在不使用Perl正则表达式的情况下找到文件中的特定行。
领取专属 10元无门槛券
手把手带您无忧上云