Match a pattern starting with sub-pattern using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match a pattern starting with sub-pattern using sed
# 1  
Old 06-26-2011
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.
# 2  
Old 06-26-2011
Try:
Code:
awk '$1~"^1\\.05"{print $1}' file > out.file

# 3  
Old 06-26-2011
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.
# 4  
Old 06-26-2011
Try:
Code:
sed -n 's/^\(1\.05[0-9]*\).*/\1/p' file > out.file

# 5  
Old 06-26-2011
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:
Code:
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

Last edited by Franklin52; 06-27-2011 at 03:05 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 06-26-2011
To be honest I just started learning sed Smilie This looks like a nice tutorial for it - Sed - An Introduction and Tutorial
This User Gave Thanks to bartus11 For This Post:
# 7  
Old 06-27-2011
Т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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Shell Programming and Scripting

Sed Pattern Match

Hi, I would like to use SED to do the following string replacement: asd1abc to www1cda asd2abc to www2cda ... asd9abc to www9cda I can use 'asd.abc' to find the orignal string, however I don't know how to generate the target string. Any suggestion? Thanks, ... (2 Replies)
Discussion started by: mail4mz
2 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question