Questions tagged [text-processing]

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.)

1190 questions
952
votes
8 answers

Find and replace text within a file using commands

How can I find and replace specific words in a text file using command line?
Jon Doe
  • 11,777
182
votes
8 answers

How to find a text file which contains a specific word inside (not in its name)

I want to find a text file in my hard disk which contains a specific word. Prior to Ubuntu 12.4 I used to start in the dash an application, I think it was called "Search for file...", whose icon was a magnifying glass.I can't find that simple…
SomeoneMe
  • 1,821
157
votes
14 answers

How to compare two files

I want to compare two files line by line and by column number 2. How could I accomplish this? File_1.txt: User1 US User2 US User3 US File_2.txt: User1 US User2 US User3 NG Output_File: User3 has changed
Roboman1723
  • 2,975
  • 8
  • 26
  • 32
110
votes
5 answers

How to separate fields with space or tab in awk

While playing with awk I came to execute: ls -la >> a.txt ; awk {'print $5 $1'} a.txt ; This is giving output like: 53277-rw------- 52347-rw------- How can I get a space between these two friends of output?
Raja G
  • 105,327
  • 107
  • 262
  • 331
110
votes
5 answers

How do I fetch only numbers in grep?

I have file like this : other lines . . . blah blah blah (:34) I wish to find the occurrence of numbers in the above file. I came up with: grep [0-9] filename But that is printing the whole: blah blah blah (:34) Rather I want only 34. Is…
Ant's
  • 3,930
105
votes
8 answers

How to remove lines from the text file containing specific words through terminal?

How to remove all the lines from the text file containing the words "cat" and "rat"?
PersonX
  • 1,127
68
votes
7 answers

Grep searching two words in a line

I've been trying to find a way to filter a line that has the word "lemon" and "rice" in it. I know how to find "lemon" or "rice" but not the two of them. They don't need to be next to the other, just one the same line of text.
Sebastian
  • 785
52
votes
4 answers

How to grep for groups of n digits, but no more than n?

I'm learning Linux, and I have a challenge that I seem to fail to solve on my own. Here it is: grep a line from a file which contains 4 numbers in a row but not more than 4. I'm not sure how to approach this. I can search for specific numbers but…
Buddha
  • 539
49
votes
6 answers

How to replace spaces with newlines/enter in a text-file?

I have simple text file named "example". Reading with terminal command: cat example Output: abc cdef ghi jk lmnopq rst uv wxyz I want to convert (transform) into following form: (expected output from cat…
Pandya
  • 37,289
47
votes
12 answers

How do I combine all lines in a text file into a single line?

I want to get all lines in a text into one line. I'm a beginner at coding trying to learn by doing. I've spent four hours trying to solve this problem. I know there's a simple solution to this problem. Here's what I've been trying. sed -e…
John
  • 579
43
votes
8 answers

How to display a random line from a text file?

I am trying to write a shell script. The idea is to select a single line at random from text file and display it as an Ubuntu desktop notification. But I want different lines to be selected each time I execute the script. Is there any solution to do…
Anandu M Das
  • 2,303
33
votes
4 answers

How can I get a list with the packages selected by apt-get autoremove?

Is there any command that prints only the name of the packages that apt-get autoremove selects? I'm creating a script that updates the kernel, removes the old kernel and the unnecessary packages (apt-get autoremove), but I want to print on the…
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
  • 18,499
30
votes
7 answers

Script or function to return how many days from now until a given date

I would like to write a script or function to tell me how many days from now until a given date in the future. What I'm struggling to work out is how to process the given date and compare it with the current date... I'm imagining something like read…
Zanna
  • 72,312
29
votes
4 answers

Bash CLI remove quotes from output of a command

I am trying to load a JSON file using jq per here. It is quite straightforward and this works: $ cat ~/Downloads/json.txt | jq '.name' "web" However, I need to assign the output of this variable to a command. I tried to do this and this works: $…
edesz
  • 803
1
2 3
79 80