Lacer
Lacer

Reputation: 5958

sed - get only text in between without spaces

How do I get the text in between the - in this SED statement?

Through

sed -n 's/.* - \\([^.]*\\).*/\\1/p'

File content

2021-08-25_@lumpsum_7000607227442711813 - App-using - ZMRUGEoLc_.mp4

Actual

ZMRUGEoLc_

Desired (without beginning and ending space)

App-using

Upvotes: 1

Views: 780

Answers (2)

sseLtaH
sseLtaH

Reputation: 11237

You could use this sed to extract the match

sed -E 's/.[^A-Z]*(.[^ ]*).*/\1/' input_file
sed -E 's/.*- (.[^ ]*) .*/\1/' input_file

Output

$ sed -E 's/.[^A-Z]*(.[^ ]*).*/\1/' input_file
App-using

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626950

With sed, you can use

sed -n 's/.* - \(.*\) - .*/\1/p' file

Here, .* - \(.*\) - .* matches any text, space+-+space, captures any text up to space+-+space and then matches the rest of the string. The whole match is replaced with the captured value and this value is printed.

See the online demo:

#!/bin/bash
s='2021-08-25_@lumpsum_7000607227442711813 - App-using - ZMRUGEoLc_.mp4'
sed -n 's/.* - \(.*\) - .*/\1/p' <<< "$s"
# => App-using

With awk, you could use

awk -F' - ' '{print $2}' file

See this online demo:

#!/bin/bash
s='2021-08-25_@lumpsum_7000607227442711813 - App-using - ZMRUGEoLc_.mp4'
awk -F' - ' '{print $2}' <<< "$s"
# => App-using

Here, the field separator pattern is set to space+-+space and Field 2 is printed.

Upvotes: 1

Related Questions