0

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)

1

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
system
cs-server@:~/$
| improve this answer | |
  • Hi, may i know what -f8 means? – mmaz Jul 13 '17 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" – metamorphling Jul 13 '17 at 2:08
  • @mmaz. Look at the man page for the cut utility. – fpmurphy Jul 13 '17 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 '17 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". – metamorphling Jul 13 '17 at 2:34
1

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
system/mode/output/file.txt
$ echo ${b%%/*}
system
$ 

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?

| improve this answer | |
  • This is useful, but b=${path##*/summary/} is probably closer to the OP's requirements. – tripleee Jul 13 '17 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 '17 at 10:31
0

Using Bash ERE support:

$ echo $FILE
/a/b/c_12/d/e/summary/system/mode/output/file.txt
$ [[ $FILE =~ (\/[a-zA-Z_0-9]*){7} ]]
$ echo ${BASH_REMATCH[1]:1}
system

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

$ echo $FILE
/a/b/c_12/d/e/f/g/summary/system/mode/output/file.txt
$ [[ $FILE =~ (*\/summary\/[a-zA-Z_0-9]*){1} ]]
$ echo ${BASH_REMATCH[2]:9}
system
| improve this answer | |
0

AWK:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.