How do i grep a pattern from part of LINE


 
Thread Tools Search this Thread
Operating Systems Solaris How do i grep a pattern from part of LINE
# 1  
Old 01-04-2008
How do i grep a pattern from part of LINE

Hi how do i grep only a part of the line from a file from all the lines.


file:
asdfgh 543212376 nag
lkjh abhilash 543757858

How do i grep and print only 543212376 and 543757858

Can i grep something like 543* and print only that.
# 2  
Old 01-04-2008
use grep and pipe it thru to either cut or awk
# 3  
Old 01-05-2008
sed may be a better option

Code:
sed 's/^.*\(543[0-9]*\).*$/\1/' file

# 4  
Old 01-05-2008
Quote:
Originally Posted by 123nagendrabhi
Hi how do i grep only a part of the line from a file from all the lines.


file:
asdfgh 543212376 nag
lkjh abhilash 543757858

How do i grep and print only 543212376 and 543757858

Can i grep something like 543* and print only that.
Code:
while read line
do
  for items in $line
  do
    case $items in 
    543* ) echo $items ;;
    esac    
  done
done < "file"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep a part of a line from a file

Hi, I have a file with thousands of lines as below INSERT INTO T_DIM_CLNT(CLNT_KY,CLNT_OBJ_ID,ISI_CLNT_ID,OPERN_ID,CLNT_NM,PRMRY_SIC_CD,PRMRY_SIC_DSC,RET_AGE_NBR,REC_CRT_TS,REC_DATA_EXTRC_TS,ETL_LOG_KY) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)... (5 Replies)
Discussion started by: sudhakar T
5 Replies

2. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

3. UNIX for Dummies Questions & Answers

can I grep a part of one line ?

<exp code="12556a" message="ok, fine4" displayMessage="jksdfj ksd" \> <exp code="123456a" message="ok, 2fine" displayMessage="jksdfj ksd" \> <exp code="12dfgda" message="1ok, fine" displayMessage="jksdfj ksd" \> now I want to cut code attribute and message attribute, such as ... (2 Replies)
Discussion started by: vincent_W
2 Replies

4. Shell Programming and Scripting

Grep regular expression to get part of a line

Hi I just started on GNU Grep with regex and am finding it very challenging and need to ask for help already... here is the problem, I have a page (MYFILE) which consists of the following.... <div> <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> <input type="hidden"... (2 Replies)
Discussion started by: noobie74645
2 Replies

5. Shell Programming and Scripting

How to get a part of the line(need help in using grep)

Hi, Suppose, DBconnection=jdbc: oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=x.x.x.x)(PORT=YYYY))(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=project_db1))) This is a part of a file <filename> . I Need to get the value of SERVICE_NAME from this line… The... (4 Replies)
Discussion started by: Dpu
4 Replies

6. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

7. UNIX for Dummies Questions & Answers

How to use grep to get only part of a line...

Hello All. I have an output file which contains the phrase "Total DFT Energy =" and then a number. This occurs many times in the output file, and what I want is to pipe the numbers (which are all different) to a file so I can plot them. How do I grep "Total DFT Energy =" and then get the numbers... (3 Replies)
Discussion started by: EinsteinMcfly
3 Replies

8. Shell Programming and Scripting

Grep part of the line

How do I print only the values of the variables from set or env. For example I have the lines USER=USER XYZ='text' SHELL=/bin/bash How do I print only the part after or starting from = Let's say I have those lines in a list X If I run Y = echo $X | grep -o "=*$" Y won't contain... (3 Replies)
Discussion started by: Transsive
3 Replies

9. Shell Programming and Scripting

Search for a pattern in part of the line

Hi guys, I need to search for a particular pattern within the first 5 chars of the line. If the pattern is found then output the whole line. Unfortunately grep only searches the whole line For example if i am searching for aaaaa in these lines aaaaabbbbbccccc bbbbbcccccaaaaa grep... (6 Replies)
Discussion started by: pineapples
6 Replies

10. Shell Programming and Scripting

grep a part of a line

Hi, In a file FILE, there is a line like this one MOLECULE C2 H2 I want to extract "C2 H2". I can do it in two step in a script : VARIABLE="`grep MOLECULE FILE`" # Assign "MOLECULE C2 H2" VARIABLE=`echo ${VARIABLE/"MOLECULE "}` # Remove "MOLECULE ". Then, $echo $VARIABLE gives C2... (6 Replies)
Discussion started by: tipi
6 Replies
Login or Register to Ask a Question