Hund

A collection of handy ways of manipulating text in Bash

February 11, 2019

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

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

$ echo "one two three" | awk '{print $3}'
three
$ 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
$ echo -e "one\ntwo\nthree" | sed -n 1p
one
$ 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.

From an output:

$ echo -e "one\ntwo\nthree" | head -10

From a file:

$ head -10 <file>

From an output:

$ echo -e "one\ntwo\nthree" | tail -10

From a file:

$ tail -10 <file>

To reverse this replace p with !p.

$ echo -e "one\ntwo\nthree" | sed -n '/two/p'
two
$ 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

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
$ echo "this is an example,string" | cut -d "," -f1
this is an example
$ echo "this is an example,string" | rev | cut -d "," -f1 | rev
string

The line will be the same width as your terminal.

printf "%`tput cols`s"|tr ' ' '-')
---------------------------------------[...]
$ cat $FILE | sed -n '/EXAMPLE_WORD_1/,/EXAMPLE_WORD_2/p'
$ echo "Example" | rev
elpmaxE
echo -n "Example" | head -c 3
Exa
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

Meta

No Comments

Use the e-mail form, if you wish to leave feedback for this post. Markdown is supported. [Terms of service]