Alex Brodov
Alex Brodov

Reputation: 3515

Bash get fields with cut

I have these string:

./ABC/ABC/SRC/test.sh
./ABC/ABC/SRC/test.sh.t
./ABC/ABC/SRC/test.cpp
./ABC/ABC/SRC/test.g.j
./ABC/ABC/SRC/test.l.am.c

I have a lot of paths of files, that are beginning with .. I want to get the whole line except the . at the beginning. As you can see the delimiter is .. I've tried it with cut:

for path in "${path[*]}"; do PathToCheck=(`$path | cut -d"." -f1,-f2`)

This is not working really well because I don't know how many dots I'll have in the file name, is there any way to say that I want from field 1 till field 'n' or till the end of the string , I want to be able use cut and sed or awk as it's much more efficient when I'm processing thousands of paths.

Upvotes: 0

Views: 49

Answers (1)

Marki555
Marki555

Reputation: 6860

If you want to get rid only of the dot at the beginning, use this:

echo "./ABC/ABC/SRC/test.sh" | cut -c2-

Result: /ABC/ABC/SRC/test.sh

This will print everything except the first character of each line (no matter if it is dot or not).

EDIT:

Bash-only version:

string="./ABC/ABC/SRC/test.sh"
echo ${string#.}

This will strip dot from the beginning of $string variable. If there is no dot, it won't do anything.

Upvotes: 3

Related Questions