Extract a pattern from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a pattern from file
# 1  
Old 06-09-2010
Extract a pattern from file

In my file I have a pattern
ri="234"

value of ri can be any i want to find this pattern in file replace this value of ri with another value stored in some variable say newri

Please tell how to do it?

Thanks in Advance
# 2  
Old 06-09-2010
Maybe this Smilie
Code:
$ cat rifile
rg="333"
rh="222"
ri="234"
rj="567"
rk="890"
$ newri=777
$ sed -ie "s/^ri=.*/ri=\"$newri\"/" rifile
$ cat rifile
rg="333"
rh="222"
ri="777"
rj="567"
rk="890"
$

This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 06-09-2010
thanks for reply but in my file occurance is like

Code:
rg="333",rh="222",ri="234",rj="567",rk="890"

pls suggest a way

Last edited by Franklin52; 06-09-2010 at 07:44 AM.. Reason: Please use code tags!
# 4  
Old 06-09-2010
Try
Code:
sed -ie "s/ri=\"[0-9]*\"/ri=\"$newri\"/" rifile



---------- Post updated at 12:48 ---------- Previous update was at 12:45 ----------

Code:
$ newri=111
$ echo 'rg="333",rh="222",ri="234",rj="567",rk="890"' | sed  "s/ri=\"[0-9]*\"/ri=\"$newri\"/"
rg="333",rh="222",ri="111",rj="567",rk="890"
$

This User Gave Thanks to pseudocoder For This Post:
# 5  
Old 06-09-2010
facing following error


Code:
sed: illegal option -- i
Usage:  sed [-n] script [file...]
sed [-n] [-e script]...[-f script_file]...[file...]


Last edited by Franklin52; 06-09-2010 at 08:34 AM..
# 6  
Old 06-09-2010
All right, looks like your version of sed does not support the so-called inplace editing.
No problem. Try
Code:
sed "s/ri=\"[0-9]*\"/ri=\"$newri\"/" input > output
mv output input

# 7  
Old 06-09-2010
now comes this error

Code:
sed: command garbled: s/ri="[0-9]*"/ri="

what i did is you can see below

Code:
sed  "s/ri=\"[0-9]*\"/ri=\"$newri\"/" $2 > output
mv output $2


Last edited by Franklin52; 06-09-2010 at 08:34 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract file name based on the pattern

Hello All, I have multiple files in a hadoop /tmp/cloudera directory. Filename are as follows ABC_DATA_BAD5A_RO_F_20161104.CSV ABC_DATA_BAD6C_VR_F_20161202.CSV ABC_DATA_BAD7A_TR_F_20162104.CSV ABC_DATA_BAD2A_BR_F_20161803.CSV ABC_DATA_BAD3T_KT_F_20160106.CSV I just need filenames... (6 Replies)
Discussion started by: prajaktaraut
6 Replies

2. Shell Programming and Scripting

Extract pattern from file

hi, file is having entries like below NRTMRC1=some value NRTSMO1=some value NRTMAA1=some value NRTSCO1=some value NRTSSMA1=some value NRTMRC11=some value NRTSMO11=some value NRTMAA11=some value NRTSCO11=some value NRTSSMA11=some alue NRTSSMMAA11=some value MNRMRC1=some value... (4 Replies)
Discussion started by: deepakiniimt
4 Replies

3. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

Extract a pattern from multiple lines in a file

I have a file that has some lines starts with * I want to get these lines, then get the word between "diac" and "lex". ex. file: ;;WORD AlAx *0.942490 diac:Al>ax lex:>ax_1 bw:Al/DET+>ax/NOUN+ gloss:brother pos:noun prc3:0 prc2:0 prc1:0 prc0:Al_det per:na asp:na vox:na mod:na gen:m num:s... (4 Replies)
Discussion started by: Viernes
4 Replies

5. Shell Programming and Scripting

extract a pattern from a xml file

Hello All, I want to write a shell script for extracting a content from a xml file the xml file looks like this: <Variable name="moreAxleInfo"> <type> <Table> <type> <NamedType> <type> <TypeRef... (11 Replies)
Discussion started by: suvendu4urs
11 Replies

6. Shell Programming and Scripting

Extract a pattern from xml file

Hi, In a single line I have the below xml content <lst name="responseHeader"><int name="status">0</int><int name="QTime">1</int></lst><lst name="status"><lst name=""><str name="name"/><str name="instanceDir">/var/www/search/current/Search/solr/./</str><str... (5 Replies)
Discussion started by: ashokvpp
5 Replies

7. Shell Programming and Scripting

I need extract column pattern in file

Hi, it's my first time in this site. I've a file that look likes Edges 21 82 Edges 3 22 Edges 34 12 Edges 1 24 Edges 6 2 Edges 12 22 etc. I need extract just the second and third column with the space between them. Thanks:) Please use code tags next time for your code and data. (4 Replies)
Discussion started by: iMunk
4 Replies

8. Shell Programming and Scripting

Extract Specific pattern - log file

Hello everyone, I am on AIX (6.1). I can only use shell (ksh) script. I can't do this on my own, so will do my best to explain my needs.I also do not know what is the best idea to make it work, so here is what I am thinking, but I may wrong. I need help to extract info on... (3 Replies)
Discussion started by: Aswex
3 Replies

9. Shell Programming and Scripting

extract pattern from csv file

hi, i have a situation where i have a csv file in the format date,name eg: 1284631889869,a 1284631889879,b 1284631889459,c . . . . . . . now i take a time and an interval from user. (5 Replies)
Discussion started by: niteesh_!7
5 Replies

10. Shell Programming and Scripting

Extract specific pattern from a file

Hi All, I am a newbie to Shell Scripting. I have a File The Server Name XXX002 ------------------------- 2.1 LAPD Iface Id Link MTU Side ecc_3_1 4 Up 512 User ecc_3_2 5 Up 512 User The Server Name XXX003 ------------------------- 2.1 LAPD (4 Replies)
Discussion started by: athreyavc
4 Replies
Login or Register to Ask a Question