Remove/replace the very first character/symbol match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove/replace the very first character/symbol match
# 1  
Old 07-24-2014
Remove/replace the very first character/symbol match

Code:
cat file.txt
file 1123.x July 23:222 /cd/hh2/k39/ss2/f7d8d9d8e6r5t4s/dd2/e/s7a/s7a2afa5017d8b975-1.7-1395610245-b22e19bbc477b134

i wish to only extract out the 1.7 (anything within the first - -)
i try to look for the sed command under match the first occurence of pattern but out of luck, my output result always match under last of - (before b22) and have everything removed.
# 2  
Old 07-24-2014
This should be what you want:

Code:
sed 's/^\([^-]*\)-[^-]*-/\1--/' file.txt

^ beginning of line
\([^-]*\) any number of non-hyphen chars saved as \1
- hyphen
[^-] any number of non-hyphen chars (this is your 1.7 STRING)
- hypen
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-24-2014
thanks.
but i think my explanation was wrong in the first place,
actually i wanted to output only 1.7 (remove everything but 1.7)
# 4  
Old 07-24-2014
This is what you want then.

Code:
sed -e 's/^[^-]*-//' -e 's/-.*//' file.txt

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 07-24-2014
i try to do some modification on top of your first solution. it worked but i will keep improving on that.
thanks
Code:
echo "file 1123.x July 23:222 /cd/hh2/k39/ss2/f7d8d9d8e6r5t4s/dd2/e/s7a/s7a2afa5017d8b975-1.7-1395610245-b22e19bbc477b134" | sed 's/^\([^-]*\)-\([^-]*\)-/\2--/' | sed 's/\-.*$//'

# 6  
Old 07-24-2014
Yes you've got the hang of it. I took an easier way out and just deleted everything up to the first hyphen and then everything from the last hyphen to the end of the string, leaving the data you wanted.

Also note how you can use two (or more) -e commands on the one sed command line, this is more efficient as the sed executable only needs to be loaded by the shell once.
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 07-24-2014
very informative. i will keep improving on sed skill. thanks for motivating me up with the solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe. all i want is to replace the first and last pipe , remove those trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i... (4 Replies)
Discussion started by: cplusplus1
4 Replies

2. Shell Programming and Scripting

sed Character match and replace

Hello All I am struck in the issue which I want to share with all of you. What I am trying to do is For every line in a file I have to replace a particular character from the given character in a file For Example Suppose the data is 1111x2222 1111x2222 2222y3333 1111x2222 I... (4 Replies)
Discussion started by: adisky123
4 Replies

3. Shell Programming and Scripting

remove caret (^) symbol from pattern using sed

Hi, I am trying to remove the caret symbol from a bash variable. This is the variable: var="GOTAN^TOK^B"and this is the code I am trying to use to remove the caret symbol: nocarrot=`echo $var | sed -e 's/^/_/g'`This is the output intended (but not acheived with the above function):... (3 Replies)
Discussion started by: goodbenito
3 Replies

4. UNIX for Dummies Questions & Answers

To replace '(' and ')' symbol using tr or sed

I am trying to replace '(' and ')' symbol with nul text using tr command. But i am not able to get the expected output . Please help # cat test.txt 155170816-(75767Mb) # tr '(' '' < test.txt 155170816-(75767Mb) # tr ')' '' < test.txt 155170816-(75767Mb) # I want the o/p as ... (8 Replies)
Discussion started by: thomasraj87
8 Replies

5. UNIX for Advanced & Expert Users

How to remove degree symbol from the TXT files?

I have TXT files to process but they contain the degree symbols in them due to which the processing program fails on these files. I want a unix command that will remove the degree symbols from these files. I tried using the sed command: sed 's///g' filename but it did not work. This issue... (14 Replies)
Discussion started by: khedu
14 Replies

6. Shell Programming and Scripting

Remove the comment symbol ' from a file.

I want to remove the commented lines in a file identified by ' symbol at the start of each ine. A sample example will be like: Input ----- 'IFerr_flag=0THEN iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then iferr_flag=0then Output -------... (3 Replies)
Discussion started by: joyan321
3 Replies

7. Shell Programming and Scripting

remove verticalbar or pipe symbol

hi guys i have 6000 rows column the text in the column has the symbol vertical bar |. i tried some of the commands to remove it but none of the commands are reconzng this symbol. would u plz help to remove this symbol from the text with any kind of unix command u r help would be appreciated ... (9 Replies)
Discussion started by: bogu0001
9 Replies

8. Shell Programming and Scripting

replace a symbol with new line

i have file in which i want to replace a char with new line please help me out (1 Reply)
Discussion started by: RahulJoshi
1 Replies

9. UNIX for Dummies Questions & Answers

match a character in a line and replace

Hi, I have a file with large number of records. Sample below: 123456789QWERT2U 2 erter 987123678ZXCVB6Y 5 7689 934567123GHJKUI4O 7 - -- -- I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become... (3 Replies)
Discussion started by: er_ashu
3 Replies

10. UNIX for Advanced & Expert Users

Character set for Symbol.pfa font

Hi, I want to know about the characters that are available in the Symbol.pfa font file. I am using freetype library to render the fonts in Windows. What encoding should I provide to get all the symbols in the Symbol font file? It would be very helpful if i get all the characters in this... (0 Replies)
Discussion started by: pushpagiri
0 Replies
Login or Register to Ask a Question