Skip to content

Instantly share code, notes, and snippets.

@tscislo
Created April 22, 2024 07:57
Show Gist options
  • Save tscislo/ad866850d211d2e397e3cddb02acafaf to your computer and use it in GitHub Desktop.
Save tscislo/ad866850d211d2e397e3cddb02acafaf to your computer and use it in GitHub Desktop.
const fs = require('fs');
// Function to read file and extract lines starting with given prefixes
function extractLinesWithPrefixes(filePath, prefixes) {
// Read the file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
// Split the file content into lines
const lines = data.split('\n');
// Filter lines that start with any of the specified prefixes
const filteredLines = lines.filter(line => {
return prefixes.some(prefix => line.trim().startsWith(prefix));
});
// Print the filtered lines
console.log(`Lines starting with the specified prefixes:`);
filteredLines.forEach(line => console.log(line));
});
}
// Example usage: Replace 'example.txt' with the path to your file
const filePath = 'example.txt';
const prefixes = ['> nx run', '> npm run']; // Change this to the prefixes you want to filter by
extractLinesWithPrefixes(filePath, prefixes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment