This is a simple C program that extracts specific lines from a text file based on an offset and a number of lines to extract. The extracted lines are saved to a new file.
The program reads one or more input text files and writes the extracted lines to new output files. Each output filename is prefixed with "(extracted)" to distinguish it from the original file.
- Extracts lines from a text file based on a configurable offset and number of lines.
- Supports merging extracted lines into a single line separated by spaces.
- Processes multiple files in a single run.
- Creates new files with the extracted lines.
-
Compile the program:
gcc -o line_extractor line_extractor.c
-
Run the program with one or more input files:
./line_extractor file1.txt file2.txt
-
The program will create new files named "(extracted) file1.txt", "(extracted) file2.txt", etc., containing the extracted lines.
You can modify the constants OFFSET
, LINES
, and MERGE_LINES
in the source code to adjust the behavior:
#define OFFSET 3 // The number of lines to skip before starting extraction.
#define LINES 1 // The number of lines to extract after the offset.
#define MERGE_LINES true // If true, the extracted lines will be merged into a single line separated by spaces.
- C compiler (gcc, clang, etc.)
- Standard C library
- If the input file has fewer lines than the specified offset, the output file will be empty.
- The program handles basic error cases, such as missing file arguments or unreadable files.
- The program processes each file independently, creating separate output files for each input file.
- When
MERGE_LINES
is enabled, a newline character is added at the end of the merged line.
Given an input file example.txt
with the following content:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
And running the program with OFFSET
set to 1 and LINES
set to 1:
./line_extractor example.txt
The output file (extracted) example.txt
will contain:
Line 1 Line 3 Line 5 Line 7 Line 9