Happyworker
Happyworker

Reputation: 61

Sed pattern with forward slashes

I have a file with the following text:

I'm looking to get rid of the "- Content-of this /media/news/section3" or "- Content-of this /media/news/random3" and the "then **number". I want to be left with only the "Name of the file.mp4" Also sometimes the name of the file is also printed like this "Name.of.the.file.mp4"

I've tried different ways of see, but I'm just a beginner and it gets pretty confusing quick, especially with the forward slashes. Any help would be appreciated.

Upvotes: 2

Views: 2200

Answers (6)

Lars Noschinski
Lars Noschinski

Reputation: 3667

To avoid confusion with the forward slashes it helps to know that the s command of sed is not bound to /: While the usual form of the s command is s/pattern/replacement/, you can replace the forward slashes by other characters, for example s,pattern,replacement,. So, to rephrase @adayzdone's answer, you can write:

sed 's,.*/\(.*mp4\).*,\1,' /path/to/your/file.txt

Upvotes: 1

Steve
Steve

Reputation: 54392

There's no need for awk or sed. You can simply use grep:

grep -o "[^/]*\.mp4" file

Explanation:

-o, --only-matching
       Print only the matched (non-empty) parts of a matching line, with each
       such part on a separate output line.

[^/]*   Match anything not a forward slash any number of times

\.mp4   Remember to escape the dot metacharacter.

Upvotes: 0

Tedee12345
Tedee12345

Reputation: 1210

Another solution:

awk '{gsub(/[^.]*\//,""); for(i=1;i<=NF-2;i++) {printf "%s ", $i} print ""}' file

Upvotes: 0

Daniel Miladinov
Daniel Miladinov

Reputation: 1592

Assuming your file is named files.txt, and also assuming you're only interested in mp4 files, then the following sed command should work, both for names with or without dots in them:

sed -i "s/^.*\/\(.*mp4\).*$/\1/g" files.txt

I named my file files.txt and these are its contents, before and after the above command:

Before:

Content-of this /media/news/section3/S02/basic/Name of the file.mp4 then 545756.
Content-of this /media/news/section3/S02/Name of the file.mp4 then 42346.
Content-of this /media/news/random3/S02/basic/Name.of.the.file.mp4 then 543.
Content-of this /media/news/random3/S02/basic/Name of the file.mp4 then 789.

After:

Name of the file.mp4
Name of the file.mp4
Name.of.the.file.mp4
Name of the file.mp4

Upvotes: 0

adayzdone
adayzdone

Reputation: 11238

Try:

 sed 's/.*\/\(.*mp4\).*/\1/' /path/to/your/file.txt

Upvotes: 2

sampson-chen
sampson-chen

Reputation: 47269

This doesn't answer your question directly, but it might do what you need it to anyway:

If these are mp4 files on your computer that you are describing, you can get the names of the files as follows:

find /path/to/some/base/dir -type f -name "*.mp4" -exec basename {} \;

This will give you file names (not prefixed with directory paths) of all the mp4 files under /path/to/some/base/dir.


If these are actually lines from a file that you need to manipulate, the following should work, albeit a bit hacky:

awk 'BEGIN{FS="/"} {print $NF}' input_file.txt | awk '{$NF=$(NF-1)=""; print}'

Upvotes: 0

Related Questions