simple sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple sed
# 1  
Old 09-12-2008
simple sed

Hello Experts,

I am being silly here and just need someone to point out what the silliness is

I have a bunch of lines that are
12343 words here that can chage (hat:98-345) more word and numbers here

I just want to pick out the numbers after "hat:" which is in every line. I have been using
echo $thing | sed 's/\(hat:[0-9]*[0-9]-[0-9]*[0-9]*\).*/\1/'
which yields
12343 words here that can chage (hat:98-345

I just want 98-345

What am I missing?

Thanks
# 2  
Old 09-12-2008
try to use translate..

tr "(hat:" " " your_file | tr ")" " " > OUTPUT

Regards
# 3  
Old 09-12-2008
Hi,
Thanks but this does not work for me

*> cat test
12343 words here that can chage (hat:98-345) more word and numbers here
*> tr "(hat:" " " test | tr ")" " "
tr: too many arguments
Try `tr --help' for more information.
# 4  
Old 09-12-2008
sorry.... dont forget the sign in blue

tr "(hat:" " " < your_file | tr ")" " " > OUTPUT
# 5  
Old 09-12-2008
Thanks but this just replaces (hat and ) with white spaces.

I would like to use this in a script and for each line of the file get those numbers

ie.
cat test
12343 words here that can chage (hat:98-345) more word and numbers here

while read line; do
var="$(echo $line | sed magic here)"
echo "$var"
done<test


output
98-345

Thanks for the help thus far
# 6  
Old 09-12-2008
Different Regex trick -- grouping

You put the \( in the wrong place

Correct version:
echo $thing | sed 's/hat:\([0-9]*[0-9]-[0-9]*[0-9]*\).*/\1/'

You could probably recuce that to:
echo $thing | sed 's/hat:\([0-9]*-[0-9]*\).*/\1/'

Maybe even:
echo $thing | sed 's/hat:\([0-9-]*\).*/\1/'
(not too sure about the usage of '-' between '[' and ']'

\( and \) are markers where your 'group' begins and ends.
That same group is later recalled with \1

Yes, you can use multiple \( and \) and then recall those with \1, \2, \3, etc...
# 7  
Old 09-13-2008
Thanks for the help but its still not what I want.

I tried

*> set line="12343 words here that can chage (hat:98-345) more word and numbers here"
*> echo $line | sed 's/hat:\([0-9-]*\).*/\1/'
12343 words here that can chage (98-345

so now it gets rid of the "hat" which is good now if only it would get rid of the rest of "12343 words here that can chage (" too then it would be perfect

I am trying other things, I guess I just do not fully understand sed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - simple qx

Im usind se as follows, sed 's/**** DRAFT ****/ /' a.lst > b.lst '**** DRAFT ****' in a.lst goes to' ****' in b.lst Does anyone know the right syntax? Thanks!! ---------- Post updated at 11:02 AM ---------- Previous update was at 11:00 AM ---------- ... (4 Replies)
Discussion started by: ttilsch
4 Replies

2. Shell Programming and Scripting

Simple sed command

I have some troubles with this: insert (at the beginning of line) character "#" from line 5 to line 15 (3 Replies)
Discussion started by: aspire
3 Replies

3. Shell Programming and Scripting

simple sed question

hi is it possible to cut this two semicolon separated sed commands echo "string2 string3 string1" | sed s'/string1//g;s/string2//g' output: " string3 " to just one sed command without semicolon? thanks in advance funksen (10 Replies)
Discussion started by: funksen
10 Replies

4. Shell Programming and Scripting

Simple SED edit

I have output like the following: B D 20070116095820001 N D S0000579.LOG S0000582.LOG B D 20070116095750001 N D S0000574.LOG S0000576.LOG B D 20070116095734001 N D S0000570.LOG S0000573.LOG B D 20070116095705001 N D S0000569.LOG S0000569.LOG B D ... (5 Replies)
Discussion started by: rdudejr
5 Replies

5. UNIX for Dummies Questions & Answers

Simple sed command

Hi, I have the following file: --# --#line1 --#line2 --#line3 --# --#line4 --#line5 and I want to use something like: sed 's/--#/newline/g' file > newfile to substitute the lines containing only '--#', but when I try, it replaces every instance of '--#' with 'newline' and I... (7 Replies)
Discussion started by: Dave724001
7 Replies

6. UNIX for Dummies Questions & Answers

Simple Sed

Hello. Just trying to write this line to an empty file. CAT shows nothing was written. Any suggestions or answers? #!/bin/bash -x THIS=FIRSTLINE sed '1w '$THIS'' testfile cat testfile Thank you. (2 Replies)
Discussion started by: steveramsey
2 Replies

7. Shell Programming and Scripting

Simple sed??

I am obviously missing something here, but the following simple command is giving me problems: sed 'i\extratext' filename.txt I receive "sed: command garbled: i\extratext' " Any suggestions? Thanks. (6 Replies)
Discussion started by: here2learn
6 Replies

8. Shell Programming and Scripting

probably simple SED-command ...

Hello, I've got a problem with SED. It's my intention to shorten a file path (removing the file name) with the help of SED. Something like: tmp\folder1\folder2\blah.txt has to be transformed to tmp\folder1\folder2\. I suppose, it's on the tip of my tongue. Perhaps it's close to: sed... (2 Replies)
Discussion started by: sysadv
2 Replies

9. UNIX for Dummies Questions & Answers

simple sed query

hi, i would like to replace a string in a series of files with another string, without outputting to new files. is this possible? i've tried using sed, and started by trying to alter the contents of one file... sed 's/string1/string2/g' file.txt but while this does the replacement on... (2 Replies)
Discussion started by: schmark
2 Replies

10. UNIX for Dummies Questions & Answers

Simple sed question

Is there an easier way to do the following: echo "|||||||" | sed 's/||/|0|/g; s/||/|0|/g' which would give the following |0|0|0|0|0|0| If it is not run twice it will not pick up the second occurance of the || and leave it empty as in echo "|||||||" | sed 's/||/|0|/g' which would give... (3 Replies)
Discussion started by: maverick
3 Replies
Login or Register to Ask a Question