Why is this command not working? (sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why is this command not working? (sed)
# 1  
Old 09-24-2015
Why is this command not working? (sed)

Hi guys,

the command is

Code:
 echo "Online Memory                              : 32768 MB" | sed 's/.*\([0-9]+\).*/\1/'

I would expect it to print 32768, it cuts off any character to the first digit, then gets all digits in 1, cuts off the rest after the digits, and should print 32768, instead it prints everything again


of course in this case, the easiest thing would be
Code:
echo "Online Memory                              : 32768 MB" | sed 's/[^0-9]//g'

but I'm just curious why the first command doesn't work

thanks in advance

funksen
# 2  
Old 09-24-2015
Strange. It obviously doesn't match the pattern, so prints without substitution. It doesn't match [[:digit:]]either, nor any repetition factor.
It would match " [0-9]* " , i.e. surrounded by spaces, but not with a + sign...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-24-2015
thx, the problem is, instead of "*", the "+" has to be escaped "\+" in sed, didn't know this

also .* would match the digits as well, so if I don't know whats before and behind the digits, I would have to use this statement I guess
Code:
 sed 's/[^0-9]*\([0-9]\+\).*/\1/'

# 4  
Old 09-24-2015
Actually \+ is a GNU extension.

In any sed (including GNU sed) one could use:
Code:
sed 's/[^0-9]*\([0-9]\{1,\}\).*/\1/'

or
Code:
sed 's/[^0-9]*\([0-9][0-9]*\).*/\1/'

--
In GNU sed one could use the -r option:
Code:
sed -r 's/[^0-9]*([0-9]+).*/\1/'


Last edited by Scrutinizer; 09-24-2015 at 10:11 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-24-2015
thank you Scrutinizer, never thought of using the curly bracket syntax, seems the right thing to go here, expect of the obvious
Code:
sed 's/[^0-9]//g'

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed command not working

Hello There - Iam trying to get this expdp running for oracle backup. And this is the code below: ### Run the export. ### Comment out any LOGFILE parameters in the .par file. if grep -i "Logfile" /<Path>$1_$2_$3.par; then ## Comment out any LOGFILE... (7 Replies)
Discussion started by: bkilaru
7 Replies

2. UNIX for Beginners Questions & Answers

sed command not working properly

This is my sample file cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend When i use sed 's/Unix/Linux/' bipin.txt , only the first... (2 Replies)
Discussion started by: Bipin_1991
2 Replies

3. Shell Programming and Scripting

sed command not working

cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend this is my file when i use sed 's/Unix/Linux/' bipin.txt all the occurences are getting... (0 Replies)
Discussion started by: Bipin_1991
0 Replies

4. Shell Programming and Scripting

sed command not working

Hi All, I am trying to run a sed command to replace a string in a file. sed -i -e "s/$Job_status_old ,$line/Job_status_new ,$line/g" stat.txt The command wen run from the command promt works fine. But the same command does not work when its put in a script. The script is not failing... (3 Replies)
Discussion started by: samyamkrishna
3 Replies

5. Shell Programming and Scripting

sed Command not working in AIX UNIX

Hi 1st problem -------------- i have this sed command in my unix script which replaces new line and carriage return in a line with the string "&#xA" the script works fine in Linux 3.0.101-0.5, but not in AIX 1 7 , the "s/\r/\&#xA/g" replacement, replaces all the character "r" in the file.... (3 Replies)
Discussion started by: maximus_jack
3 Replies

6. Shell Programming and Scripting

sed Command new Line not working Tried many variations

Hi I have assigned an output of a command to $I. I try to print the input and put a new line after occurrence of the hostname which is assigned to $HOST1 ( Example: pwrm16 ) . First of all I need to get rid of the Colon after the host name pwrm16: and make it pwrm16 then I want to print the... (10 Replies)
Discussion started by: mnassiri
10 Replies

7. Shell Programming and Scripting

sed command working different in linux environment.

Hi I tried running the code scrname=`whence $0 | sed -e 's/\.\///g'` where $0 is substituted by cm_dsjobrun.sh in unix env then the value it returns me is SCRNAME=/data/ds/dpr_ebicm_uat/etl/cm3_0/scripts/shell/cm_dsjobrun.sh whereas i ran the same code on linux env The value... (9 Replies)
Discussion started by: vee_789
9 Replies

8. Shell Programming and Scripting

sed command not quite working yet, if anyone can help appreciated

Hi gang, I am trying to create some batch commands for many html pages I need to re-format. I am trying the number 2b in this example to wrap anchor tags around the number that will be referenced in the footnotes. I am trying to use the h/H hold command, but I have never tried using it... (2 Replies)
Discussion started by: naphelge
2 Replies

9. Shell Programming and Scripting

passing variable to sed command not working

Hello All, I am trying to embed variable in sed command to fetch a portion of record between two pattern. This command is not working ...any suggestion on this how to place the variable in sed command to find a portion . I am using Sun OS (Solaris). Thanks JM (1 Reply)
Discussion started by: jambesh
1 Replies

10. UNIX for Dummies Questions & Answers

Sed command not working

Hi, I have a test file as follows: 1G102119 ^ AA1179291 ^ 06oct2006 09:50:35^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:49:45^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:48:58^ 73.4^ 2^ 17^ 0^ 2 1G102119 ... (9 Replies)
Discussion started by: shashi_kiran_v
9 Replies
Login or Register to Ask a Question