shiril sukhadeve
shiril sukhadeve

Reputation: 31

how to use 'cut' command in linux with multi character sting

/home/user/views/links/user1/gitsrc/database/src/

This is my string. I want to cut it in 2 strings such as "/home/user/views/links/user1/" "/database/src/"

so the delim is not actally a single character but a group of characters ie "gitsrc".

Upvotes: 3

Views: 10965

Answers (1)

Freddy
Freddy

Reputation: 4688

You can only define a single character as delimiter in cut.

You could use awk where the field separator can be a single character, a null string or a regular expression, e.g.

$ echo '/home/user/views/links/user1/gitsrc/database/src/' |
  awk -F'gitsrc' '{ print $1 " " $2 }'
/home/user/views/links/user1/ /database/src/

or

$ echo '/home/user/views/links/user1/gitsrc/database/src/' |
  awk -F'gitsrc' '{ print $1 ORS $2 }'
/home/user/views/links/user1/
/database/src/

In your shell you could or use a parameter expansion to get the first and second part:

$ str=/home/user/views/links/user1/gitsrc/database/src/
$ echo "${str%%gitsrc*}"  # remove longest suffix `gitsrc*`
/home/user/views/links/user1/
$ echo "${str#*gitsrc}"   # remove shortest prefix `*gitsrc`
/database/src/

Upvotes: 5

Related Questions