Extract pattern from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract pattern from file
# 1  
Old 12-30-2014
Extract pattern from file

hi,
file is having entries like below
Code:
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
MNRSMO1=some value

i want to get record using a script, cut that row only which matches this pattern . parameters passed to scripts are first three characters in below example [these numbers can vary ,can be 4 ,5..but then those 4 or 5 characters will be passed to script,] and digit number ,
Code:
./script NRT 1 .

script should get the records as below:
Code:
NRTMRC1=some value
NRTSMO1=some value
NRTMAA1=some value
NRTSCO1=some value
NRTSSMA1=some value


Last edited by Scrutinizer; 12-30-2014 at 05:25 AM.. Reason: code tags
# 2  
Old 12-30-2014
Code:
#!/bin/bash
grep ^"$1" file|grep [A-Z]$2=

hth
# 3  
Old 12-30-2014
With one grep:
Code:
grep "^$1[^0-9]*$2=" file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-30-2014
Ensure that $2 matches left from the first =
Code:
#!/bin/sh
grep "^$1[^0-9=]*$2=" file

# 5  
Old 12-31-2014
thank you guys !
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 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

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 (10 Replies)
Discussion started by: pasricha.kunal
10 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