Questions about modifying, creating or parsing plain text files. This is a very broad tag, if your question is about using a specific tool, please tag it with the tool's name as well (e.g. sed, awk, perl, etc.)
Questions tagged [text-processing]
1170 questions
33
votes
8 answers
Prepend ## to every line in a text file
I want to add hashes to all the lines in a regular text file. I'm fine with both the use of terminal and GUI—I just need to get it done.

Oxwivi
- 17,849
28
votes
2 answers
Why is my sorted file bigger?
I have a 2958616 byte text file. When I run sort < file.txt | uniq > sorted-file.txt, I get a 3213965 byte text file. Why is my sorted text file bigger?
You can download the text files here.

wb9688
- 1,437
16
votes
3 answers
How can I copy the content of a text file and paste it to another starting at a certain line?
I need to copy the content of a text file and paste it to another text file. The first text file has 10 lines of data and I need them to be copied to the second text file starting at line number 5 (for example). So in the second text file those data…

G. Paschalis
- 173
10
votes
6 answers
Shorten lines, adding "..." ellipsis
How to cut off lines that are longer than some width,
and marking the lines that were chopped off with an ellipsis?
Only lines that were actually shortened should be
marked, but not lines that had just the right lenght in the first place.
I would…

Volker Siegel
- 13,065
- 5
- 49
- 65
8
votes
5 answers
How do I remove blank lines from files?
I have a file (hosts) with some lines without content, how do I remove that lines without content?

wb9688
- 1,437
8
votes
7 answers
How can I get lines where a specific word is repeated exactly N times?
For this given input:
How to get This line that this word repeated 3 times in THIS line?
But not this line which is THIS word repeated 2 times.
And I will get This line with this here and This one
A test line with four this and This another THIS and…

αғsнιη
- 35,660
8
votes
4 answers
How to delete words from txt file, that exists on another txt file?
File a.txt has about 100k words, each words is in new line
july.cpp
windows.exe
ttm.rar
document.zip
File b.txt has 150k words, one word by line - some words are from file a.txt, but some words are new:
july.cpp
NOVEMBER.txt
windows.exe …

Kate-Kasia
- 83
7
votes
2 answers
Append line numbers at end of each line in the file
In a peculiar case I needed some of my text files to append with line number at the end. How to achieve this?

Ramaprakasha
- 231
- 3
- 10
6
votes
6 answers
How do I remove lines of a file which contain a specific string?
How do I remove lines of a file (hosts) which contain "adf.ly" string?

wb9688
- 1,437
6
votes
5 answers
Replace the same text in more than 200 files in the same folder
I have more than 200 files in one folder on Ubuntu 22.04. I would like to search these for a specific text. If the text is found, it should be replaced by another text.
How can I achieve this?

D-T
- 383
5
votes
6 answers
Parsing a file using text processing tools
A file looks like:
1140.271257 0.002288454025 0.002763420728 0.004142512599 0 0 0 0 0 0 0 0 0 0 0
1479.704769 0.00146621631 0.003190634646 0.003672029231 0 0 0 0 0 0 0 0 0 0 0
1663.276205 0.003379552854 0.04643209167 0.0539399155 0 0 0 0 0 0 0 0 0…

heineman
- 51
5
votes
1 answer
How do I sort a wordlist by string length?
There is a wordlist look like this;
123
b
1
4rr
f
k3j3
gg
Then I needed to sort these words by string length (doesn't matter which start with numeric or string both are okay)
The output should be this:
b
1
f
gg
123
4rr
4rr
Is there a sort command…

Smile.Hunter
- 8,365
4
votes
2 answers
Add column to text file where the value is the row number?
How do I add a column to a text file, where the value is the row number?
File before:
Foo Ba Lk Q
Lorem ipsum doler
File after
1 Foo Ba Lk Q
2 Lorem ipsum doler
etc

user3761308
- 153
4
votes
3 answers
4
votes
5 answers
Remove letters and dots from all lines
I have a CSV that has the output of time commands of different programs including the ID of the execution.
Therefore my files contain records in the following form:
ID,execution_time
Example Record:
345,0m0.047s
I only need the number of…

Jim Blum
- 1,407
- 4
- 19
- 24