Wednesday, June 30, 2021

Get substring of a string in shell

 I wanted to get a only file names from the output of  command find . -maxdepth 1 -type d

This returns a strings contains "./" at the begining.

./1-case

./2-case

./3-case

./4-case

./5-case

./6-case


to remove first two characters i used command cut. as follows

echo "./1-case" | cut -c3-


-c means return characters

3- means from 3rd character to end


other examples

-c3 will return only 3rd character

-c3-4 will return characters form 3rd to 4th


 



No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...