testphreak
testphreak

Reputation: 499

Need help stripping off characters using sed

I have a string: abc-2.25_20141104-1586.uvw.xyz

I'd like to strip off .uvw.xyz and retain abc-2.25_20141104-1586. How can I do that using sed? Help appreciated!

Upvotes: 1

Views: 72

Answers (8)

user3897784
user3897784

Reputation:

echo "abc-2.25_20141104-1586.uvw.xyz" | sed 's/.uvw.xyz$//'

Output: 
abc-2.25_20141104-1586

Upvotes: 1

Jotne
Jotne

Reputation: 41460

Here is an awk way to do it. (It will remove any length of last w fields)

echo "abc-2.25_20141104-1586.uvwmore.xyzyes" | awk -F\. 'NF-=2' OFS=\.
abc-2.25_20141104-1586

Upvotes: 0

Khanna111
Khanna111

Reputation: 3931

$ echo "abc-2.25_20141104-1586.uvw.xyz" | sed 's/\(.*-[0-9]*\)\..*/\1/'
abc-2.25_20141104-1586

Explanation: (.-[0-9])=> Matches everything else in the beginning except the suffix you do not want. And 1 => Is the first pattern matched.

Please see: http://www.grymoire.com/Unix/Sed.html#uh-51 (Section on keeping the pattern)

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 343107

if you just know that you will always want to remove last two placeholders separated by dots:

$ string="abc-2.25_20141104-1586.uvw.xyz" 
$ string=${string%.*}
$ echo ${string%.*}

Upvotes: 0

Hackaholic
Hackaholic

Reputation: 19763

using awk:

echo "abc-2.25_20141104-1586.uvw.xyz" | awk '{gsub('/\.uvw\.xyz/',"",$0); print $0}'

other approach:

a="abc-2.25_20141104-1586.uvw.xyz"
echo ${a%.uvw.xyz}

output:

abc-2.25_20141104-1586

Upvotes: 0

Mark Steele
Mark Steele

Reputation: 53

You need not use sed. Can be accomplished fairly easily with the basename command:

basename abc-2.25_20141104-1586.uvw.xyz .uvw.xyz

Should output

abc-2.25_20141104-1586

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53565

echo "abc-2.25_20141104-1586.uvw.xyz" | sed 's/\.[a-z]\{3\}\.[a-z]\{3\}//g'

output

abc-2.25_20141104-1586

Upvotes: 2

unxnut
unxnut

Reputation: 8839

Here is a possibility:

sed 's/\.[[:alpha:]]\{3\}\>//g'

Explanation: Start at ., followed by three alphabetic characters, followed by end of word.

Upvotes: 0

Related Questions