A collection of handy ways of manipulating text in Bash
Here’s my collection with handy ways of manipulating text in Bash. I have tested all of these commands with GNU Bash 4.4.23 (or newer). If you have any feedback or suggestions feel free to send them to me via any of the listed ways in the footer or by creating a issue on GitLab.
I have also added a version history to make it easier to keep track of the changes to this post.
- Adding words
- Printing content
- Print the third word in a string
- Print the first and third word in a string
- Print a specific line
- Print lines 10 to 20
- Print the first 10 lines
- Print the last 10 lines
- Print lines containing a specific word
- Print lines starting with specific word
- Print lines ending with specific word
- Print string up until specific character
- Print string beginning from specific a character
- Print a line of characters with the width of the client
- Print the content between two matching words.
- Print a word backwards
- Print the first 3 characters in a string
- Print the last 3 characters in a string
- Removing words
- Remove the first character of the first line
- Remove the last character of the first line
- Remove the first character of every line
- Remove the last character of every line
- Remove trailing spaces from all the lines in a file
- Remove all commented lines
- Remove all empty lines
- Remove a line by matching word
- Remove
N
number of lines in the beginning or the end.
- Replacing words
- Changing lowercase & uppercase words
- Counting
Adding words
Add a word to the beginning of a string
$ echo "is an example" | sed 's/^/this /'
this is an example
Add a word to the end of a string
$ echo "this is an" | sed 's/$/ example/'
this is an example
Add a word on a new line after a match
$ echo -e "1\n3" | sed '/1/a 2'
1
2
3
Add a word on a new line before a match
$ echo -e "1\n3" | sed '/3/i 2'
1
2
3
Add a word to a specific line
Add the words Example word
to the third line in a file:
$ sed -i '3iExample word' <file>
Printing content
Print the third word in a string
$ echo "one two three" | awk '{print $3}'
three
Print the first and third word in a string
$ echo "one two three" | awk '{print $1$3}'
one three
You can add some spacing between the words like this:
$ echo "one two three" | awk '{print $1" "$3}'
one three
Print a specific line
$ echo -e "one\ntwo\nthree" | sed -n 1p
one
Print lines 10 to 20
$ sed -n 10,20p <file>
Or pipe it with a command:
$ cat <file> | sed -n 10,20p
If you want to print all lines but lines 10 to 20 replace p
with d
.
Print the first 10 lines
From an output:
$ echo -e "one\ntwo\nthree" | head -10
From a file:
$ head -10 <file>
Print the last 10 lines
From an output:
$ echo -e "one\ntwo\nthree" | tail -10
From a file:
$ tail -10 <file>
Print lines containing a specific word
To reverse this replace p
with !p
.
$ echo -e "one\ntwo\nthree" | sed -n '/two/p'
two
Print lines starting with specific word
$ echo -e "one\ntwo\nthree" | sed -n '/^one/p'
one
Same thing but with several words:
$ echo -e "one\ntwo\nthree" | sed -n '/^one\|^three/p'
one
three
Print lines ending with specific word
To reverse this replace p
with !p
.
$ echo -e "one\ntwo\nthree" | sed -n '/one$/p'
one
three
Same thing but with several words:
$ echo -e "one\ntwo\nthree" | sed -n '/one$\|three/p'
one
three
Print string up until specific character
$ echo "this is an example,string" | cut -d "," -f1
this is an example
Print string beginning from specific a character
$ echo "this is an example,string" | rev | cut -d "," -f1 | rev
string
Print a line of characters with the width of the client
The line will be the same width as your terminal.
printf "%`tput cols`s"|tr ' ' '-')
---------------------------------------[...]
Print the content between two matching words.
$ cat $FILE | sed -n '/EXAMPLE_WORD_1/,/EXAMPLE_WORD_2/p'
Print a word backwards
$ echo "Example" | rev
elpmaxE
Print the first 3 characters in a string
echo -n "Example" | head -c 3
Exa
Print the last 3 characters in a string
echo -n "Example" | tail -c 3
ple
The flag -n
is to make sure that Bash doesn’t add a newline
at the end.
Removing words
Remove the first character of the first line
$ echo -e "example\nwords" | sed '0,/./s/./img/'
xample
words
Remove the last character of the first line
$ echo -e "example\nwords" | sed '0,/./s/.$/img/'
exampl
words
Remove the first character of every line
$ echo -e "example\nwords" | sed 's/./img/'
xample
ords
Remove the last character of every line
$ echo -e "example\nwords" | sed 's/.$/img/'
exampl
word
Remove trailing spaces from all the lines in a file
$ sed -i 's/^[ \t]*/img/;s/[ \t]*$/img/' <file>
Remove all commented lines
$ sed -i 's/#.*/img/' <file>
Remove all empty lines
$ sed -i '/^$/d' <file>
Remove a line by matching word
In a file:
$ sed '/EXAMPLE_WORD/d' ./file.txt
In a output:
$ cat file.txt | sed '/EXAMPLE_WORD/d'
Remove N
number of lines in the beginning or the end.
Remove the last line at the end:
$ cat file.txt | head -n -1
Remove the first line at the top:
$ cat file.txt | tail -n +2
Replacing words
Replace the first occurrence of a word
$ echo -e "one two four" | sed 's/four/three/'
one two three
Replace the second occurrence of a word
$ echo -e "one two three three" | sed 's/three/four/2'
one two three four
Replace all occurrences of a word
$ echo -e "dog cat cat" | sed 's/cat/dog/g'
dog dog dog
Replace a word on line 3
$ sed -i '3 s/cat/dog/' <file>
Replace all occurrences of a word on lines 3-10
$ sed -i '3-10 s/cat/dog/g' <file>
Replace whole line with matched pattern
echo -e "1\n4\n3" | sed '/4/c 2'
1
2
3
Replace all tabulator spaces with 4 regular spaces in a file
$ sed -i 's/\t/ /g' <file>
Changing lowercase & uppercase words
Change all words to lowercase
$ echo "EXAMPLE Text" | awk '{print tolower($0)}'
example text
Change all words to uppercase
$ echo "Example text" | awk '{print toupper($0)}'
EXAMPLE TEXT
Change the first letter to lowercase
$ echo "example" | sed 's/.*/\l&/'
Example
Change the first letter to uppercase
$ echo "example" | sed 's/.*/\u&/'
Example
Change the last letter to lowercase
$ echo "ExamplE" | sed 's/.$/\l&/'
Example
Change the last letter to uppercase
$ echo "EXAMPLe" | sed 's/.$/\u&/'
Example
Counting
Count the number of lines
$ cat file.txt | wc -l
Count the number of words
$ cat file.txt | wc -w
Count the number of characters
$ cat file.txt | wc -m