Bash print word after match

bash, match, regex, sed

Solution

Superficially, you should be using:

hive -S -e "desc formatted table" |
sed -n -e 's/^.*Database: //p'

This will show the complete line containing `Database:`. When you've got that working, you can eliminate the unwanted material on the line too.

Alternatively, you could use:

echo "$output" |
sed -n -e 's/^.*Database: //p'

Or, again, given that you're using Bash, you could use:

sed -n -e 's/^.*Database: //p' <<< "$output"

I'd use the first unless you need the whole output preserved for rescanning. Then I'd probably capture the output in a file (with `tee`):

hive -S -e "desc formatted table" |
tee output.log |
sed -n -e 's/^.*Database: //p'

Problem

I have a variable that stores the output of a file. Within that output, I would like to print the first word after `Database:`. I'm fairly new to regex, but this is what I've tried so far: ``` sed -n -e 's/^.*Database: //p' "$output" ``` When I try this, I am getting a `sed: can't read prints_output: File name too long` error. Does `sed` only take in a filename? I am running a hive query to `desc formatted table` and storing the results in `output` like so: ``` output=`hive -S -e "desc formatted table"` ``` `output` is then set to the result of that: ``` ... # Detailed Table Information Database: sample_db Owner: sample_owner CreateTime: Thu Feb 26 23:36:43 PDT 2015 LastAccessTime: UNKNOWN Protect Mode: None Retention: 0 Location: maprfs:/some/location Table Type: EXTERNAL_TABLE Table Parameters: ... ```

Original source

Related problems