Match a pattern starting with sub-pattern using sed

Hi all,

I've been experiencing a difficulty trying to match a number and write it to a new file.

My input file is: input.txt
It contains the lines:

103P 123587.256971 3.21472112 3.1517423
1.05897234566427 58.2146258 12.35478 25.3612489

What would be the sed command to match the number 1.05897234566427

Note, that I only know that the pattern starts with 1.05, and I would like to match it and write it along with the rest numbers after 1.05 into another file (output.txt), but not the entire line!!!

Is it possible sed to handle something like this? If you know just the first 2 or 3 digits of a number, to match the whole number.

Finally, the output file must contain 1.05897234566427 only.

I would appreciate your help

Thank you in advance.

Try:

awk '$1~"^1\\.05"{print $1}' file > out.file

Thanks very much Bartus11. The code you've posted works perfectly fine.
I would be grateful if you could show the "sed" version of the code.

Once again, thank you for your help though.

Try:

sed -n 's/^\(1\.05[0-9]*\).*/\1/p' file > out.file

Thanks again,
this works fine too.
Where did you learn all these? Could you recommend a good tutorial of sed?
I must confess that I spent few days in trying to solve that, unfortunately unsuccessful.

My original code was:

sed -n "s/\(1.05*\).*/\1/w file"  out.file

but it printed the entire line.

I need to grasp the logic of all that.

Regards

To be honest I just started learning sed :wink: This looks like a nice tutorial for it - Sed - An Introduction and Tutorial

1 Like

hank you very much for the link. I'll pay a special attention to it.

Regards.

---------- Post updated at 11:49 AM ---------- Previous update was at 12:38 AM ----------

hi again guys.

How about if I want to do some arithmetic with my values in the file above, using sed?
For example: If I would like to increase or decrease the number 1.05897234566427 with the amount of 0.001. So, the final result to be 1.05997234566427 or 1.05797234566427.
I've tried the following but unsuccessful:

q=0.001

sed -n 's/^\(1\.05[0-9]*\).*/expr ^\(1\.05[0-9]*\).*+$q/p  file > out.file

Unfortunately, I ged: expr ^\(1\.05[0-9]*\).*+$q instead of 1.05997234566427.

Any ideas how to solve that.

I need that solution since, I am going to embed sed inside a loop which should change certain values approximately 100000 times. It will rather huge loop.

Thanks in advance.

try this...

$ sed -n 's/^\(1\.05[0-9]*\).*/\1/p' <input_file> | sed "s,^,echo \"scale=25; ,g;s,$, + $q\" | bc,g" | sh (for addition)
1.05997234566427

$ sed -n 's/^\(1\.05[0-9]*\).*/\1/p' <input_file> | sed "s,^,echo \"scale=25; ,g;s,$, - $q\" | bc,g" | sh (for subtraction)
1.05797234566427

Hi Jayan_jay,

when I run one of the commands above I get:

sed -n 's/^\(1\.05[0-9]*\).*/\1/p' <input_file> | sed "s,^,echo \"scale=25; ,g;s,$, + $q\" | bc,g" | sh

./testshel.sh: line 10: syntax error near unexpected token `|'
./testshel.sh: line 10: ` sed -n 's/^\(1\.05[0-9]*\).*/\1/p' <small.in> | sed -n "s,^,echo \"scale=25; ,g;s,$, + $q\" | bc,g"'

Any idea what am I doing wrong?

is your grep supports -o option, then you can try this command

 
grep -o "^1.05" filename

i am getting the output.. anyway try the below. might this help u.$ cat test.shq='0.001'sed -n 's/^\(1\.05[0-9]*\).*/\1/p' input_file | sed "s,^,echo \"scale=25; ,g;s,$, - $q\" \| bc,g" | sh$ ./test.sh1.05797234566427$

You could also go with Perl:

perl -slane 'if (/^1\.05\d*/){$F[0]+=$q;print $F[0]}' -- -q=0.001 file > out.file

Thank you all for your help, but just the example given by 'bartus11' worked for me.

But I really need an working sed version of this.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Jayan_jay, it's really weird that the above command works for you but not for me.

sed -n 's/^\(1\.05[0-9]*\).*/\1/p' <input_file> | sed "s,^,echo \"scale=25; ,g;s,$, + $q\" | bc,g" | sh
This is the code you gave me.

I just copy and paste your code into terminal and just replace <input_file> with <small.in> which is my original file.

What does this ' | sh' at the end of the line actually do?

I am getting this error:
bash: syntax error near unexpected token `|'

But nevertheless, thank you for you help though.

I am just passing the output to the shell (sh) to execute the syntax.Else you just try and assign the content(till before sh) to a variable and then execute it ..q='0.001'variable=`sed -n 's/^\(1\.05[0-9]*\).*/\1/p' j | sed "s,^,echo \"scale=25; ,g;s,$, - $q\" \| bc,g"`eval $variable

Can you tell me, where should the input file name be positioned in the above code?

I have just mentioned "j" as the input filename .. Just replace that with ur input filename.

hi jayan_jay,

The problem has just been solved. It turned out the <file_name> should be just file_name, without arrows. Both codes you have previously posted work fine.
I am so sorry for my incompetence and for manipulating the time of all of you.

Regards

Not a problem.. we always there to help.. Welcome .. !!