SED Replacing all but one regex match on a line or specific matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED Replacing all but one regex match on a line or specific matches
# 1  
Old 05-02-2011
SED Replacing all but one regex match on a line or specific matches

Hi,

I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ".

File.name.is.long.ext -> File name is long.ext

I can get the desired effect with

Code:
echo "File.name.is.long.ext" | sed 's/\./ /g;s/ /\./4'

Is it possible to do the same thing with just one command?

The following type of logic doesn't work
Code:
echo "File.name.is.long.ext" | sed 's/\./ /1~3'

# 2  
Old 05-02-2011
Code:
echo "File.name.is.long.kdk.dkkd.ext" | sed 's/\./ /g;s/\(.*\) /\1\./'

# 3  
Old 05-02-2011
or

Code:
echo "file.name.is.long.ext" | sed -e 's/\(.*\)\.\(.*\)/\1 \2/;'

or

Code:
echo "file.name.is.long.ext" | perl -wlpe 's/(.*)\.(.*)/$1 $2/;'

# 4  
Old 05-02-2011
Code:
 echo "File.name.is.long.ext"  |sed ':a;s/\(.*\)\.\(.*\)\(\..*\)/\1\2\3/;ta'
Filenameislong.ext

# 5  
Old 05-02-2011
Thanks for the quick replies. What you gave was not quite the desired result, but you got me on the right track. I found the following term works, but man it's long Smilie.

Code:
echo "file.name.is.long.ext" | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1 \2 \3 \4.\5/'

It's too bad that sed didn't have a match for say...change all matches except the 4th one. You can change the 4th match with
Code:
sed 's/asdf/fdsa/4'

but there doesn't appear to be a short form to do the opposite.
# 6  
Old 05-02-2011
Quote:
Originally Posted by vectox
Thanks for the quick replies. What you gave was not quite the desired result, but you got me on the right track. I found the following term works, but man it's long Smilie.

Code:
echo "file.name.is.long.ext" | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1 \2 \3 \4.\5/'

It's too bad that sed didn't have a match for say...change all matches except the 4th one. You can change the 4th match with
Code:
sed 's/asdf/fdsa/4'

but there doesn't appear to be a short form to do the opposite.

Code:
echo "File.name.is.long.ext"  |sed ':a;s/\(.*\)\.\(.*\)\(\..*\)/\1 \2\3/;ta'               File name is long.ext

---------- Post updated at 04:01 PM ---------- Previous update was at 03:52 PM ----------

or:
Code:
echo $(echo ${var%.*}|tr '.' ' ')'.'${var##*.}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

3. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

4. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Replacing specific entry using sed

Hi guys, I'm new to bash programming, so please pardon me. I'm trying to replace an entry's text in Books.txt This code works perfectly: sed -i "s/$BookTitle/$NewBookTitle/g" Books.txt But problem is, if there are double entries, it will also replace that entry. For example: ... (12 Replies)
Discussion started by: todaealas
12 Replies

6. Shell Programming and Scripting

Replacing specific characters using sed

Hi, I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:. I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the... (8 Replies)
Discussion started by: vnayak
8 Replies

7. Shell Programming and Scripting

replacing field in specific line in a file

Hi, I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's... (5 Replies)
Discussion started by: vytenis
5 Replies

8. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

9. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

10. Shell Programming and Scripting

Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily. So far I have this: rewrite ^action/static/(+)/$ staticPage.php?pg=$1&%$query_string; What I want done... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies
Login or Register to Ask a Question