I prompt user to input a path and stored in a variable named path.

$path: /a/b/c_12/d/e/summary/system/mode/output/file.txt

The structure of the path will always be the same, its just that the system, mode, output names are not fix. Therefore, im planning to grep the system name which located after /summary/.

How can i grep the system name and then stored in a variable so that i can used the variable using bash script? I am expecting to the system name should be in a new variable. (e.g. $systemname)

4 Answers 4


If up to "system" path structure is the same, then you can use cut for this task.
-d stands for delimeter, in your case it's '/'. While -f is needed to choose the cut piece.

cs-server@:~/$ systemname=$(echo "/a/b/c_12/d/e/summary/system/mode/output/file.txt" | cut -d'/' -f8)
cs-server@:~/$ echo $systemname
  • Hi, may i know what -f8 means?
    – mmaz
    Jul 13, 2017 at 2:03
  • @mmaz to put it simply, string is being cut into pieces by delimiter set with -d. Then you choose which piece you want to get returned. As in -f7 will return you "summary", while -f9 will return "mode" Jul 13, 2017 at 2:08
  • @mmaz. Look at the man page for the cut utility.
    – fpmurphy
    Jul 13, 2017 at 2:12
  • Hi, this is my code, i tried on both but then error line 7: systemname: command not found. #!bin/bash echo "Enter the path:" read path #try1 systemname = $(echo "/a/b/c_12/d/e/summary/system/mode/output/file.txt" | cut -d'/' -f8) echo $systemname #try2 systemname = $(echo "$path" | cut -d'/' -f8) echo $systemname
    – mmaz
    Jul 13, 2017 at 2:25
  • @mmaz Yours "systemname = $" should be "systemname=$", no spaces in variable declaration are needed. Also, strange you did not get an error on "#!bin/bash", I expect it to be more like "#!/bin/bash". Jul 13, 2017 at 2:34

Use bash Shell Parameter Expansion

$ path='/a/b/c_12/d/e/summary/system/mode/output/file.txt'
$ b=${path##/a/b/c_12/d/e/summary/}
$ echo $b
$ echo ${b%%/*}

See also:

How do I do string manipulations in bash?

How can I use parameter expansion? How can I get substrings? How can I get a file without its extension, or get just a file's extension? What are some good ways to do basename and dirname?

  • This is useful, but b=${path##*/summary/} is probably closer to the OP's requirements.
    – tripleee
    Jul 13, 2017 at 10:29
  • Also you shoud properly use double quotes around the argument to echo. It doesn't matter here, but there are nasty surprises when the file name contains whitespace or literal wildcard characters.
    – tripleee
    Jul 13, 2017 at 10:31

Using Bash ERE support:

$ echo $FILE
$ [[ $FILE =~ (\/[a-zA-Z_0-9]*){7} ]]
$ echo ${BASH_REMATCH[1]:1}

Given that the OP changed the requirements to: systen name can be anywhere but always after summary

$ echo $FILE
$ [[ $FILE =~ (*\/summary\/[a-zA-Z_0-9]*){1} ]]
$ echo ${BASH_REMATCH[2]:9}


var1=$(echo '/a/b/c_12/d/e/summary/system/mode/output/file.txt' | awk 'BEGIN{FS="/";}{print $8}');echo $var1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .