Skip to content

Instantly share code, notes, and snippets.

@silviaclaire
Created May 23, 2018 07:36
Show Gist options
  • Save silviaclaire/0923f816c2de6190f13aa3612018781b to your computer and use it in GitHub Desktop.
Save silviaclaire/0923f816c2de6190f13aa3612018781b to your computer and use it in GitHub Desktop.
Bash: Removing characters from strings
# https://www.shellhacks.com/remove-first-last-characters-strings-bash/
# File to modify
$ cat file
12345===I Love Bash===54321
12345===I Love Bash===54321
12345===I Love Bash===54321
# Remove First N Characters Of Each Line
# (delete first 5 characters of each line / starting from the 6th character)
$ cat file | cut -c 6-
===I Love Bash===54321
===I Love Bash===54321
===I Love Bash===54321
# Print Strings Between First and Last Characters
# (print strings between 9th and 20th characters of each line)
$ cat file | cut -c 9-20
I Love Bash
I Love Bash
I Love Bash
# Print First N Characters Of Each Line
# (print first 20 characters of each line)
$ cat file | cut -c 1-20
12345===I Love Bash
12345===I Love Bash
12345===I Love Bash
# Remove Last Character Of Each Line (Using combination of reverse and cut commands)
# (delete last character of each line)
$ rev file | cut -c 2- | rev
12345===I Love Bash===5432
12345===I Love Bash===5432
12345===I Love Bash===5432
# Remove Last N Characters Of Each Line
# (delete last 8 characters of each line)
$ rev file | cut -c 9- | rev
12345===I Love Bash
12345===I Love Bash
12345===I Love Bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment