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
# 8  
Old 06-27-2011
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
# 9  
Old 06-27-2011
Hi Jayan_jay,

when I run one of the commands above I get:
Code:
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?
# 10  
Old 06-27-2011
is your grep supports -o option, then you can try this command

Code:
 
grep -o "^1.05" filename

# 11  
Old 06-27-2011
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$
# 12  
Old 06-27-2011
You could also go with Perl:
Code:
perl -slane 'if (/^1\.05\d*/){$F[0]+=$q;print $F[0]}' -- -q=0.001 file > out.file

# 13  
Old 06-27-2011
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.
# 14  
Old 06-27-2011
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
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