grep and cut problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep and cut problem
# 1  
Old 07-23-2008
grep and cut problem

Hello folks,
I have to (e)grep out a certain pattern e.g.
Code:
 <TAG1> from a huge log file which does not have any space as such.
The thing is that once I have 'grep'ed out the <TAG1> from the file I need to extract the content within the tags, i.e,
<TAG1>Data_To_Be_Extracted</TAG1>

The underlined data needs to be extracted.
The problem is that with such a huge file with big wide lines with lots of tags I can't get hold of any delimiter.
Thanks in advance.
# 2  
Old 07-23-2008
Code:
sed 's/.*<TAG1>\([^>]*\)<\/TAG1>.*/\1/g'

# 3  
Old 07-23-2008
I am sorry Zaxxon but wont this be just replacing the stuff? Please forgive my incompetence with sed. Is there any subString sort of functionality in UNIX?
# 4  
Old 07-23-2008
Question Can you provide a sample of the file?

It can often be easier to understand when we see actual data, as opposed to answering a theoretical question.
# 5  
Old 07-23-2008
Nodding to joeyg, I can say that the sed code was working with your example, see yourself:

Code:
root@isau02:/data/tmp/testfeld> cat infile2
somelala.awno2irbn8sp+0<TAG1>Data_To_Be_Extracted</TAG1>some more data or is it rubbish or blaapowjdaowih2
xawmwduhawudhguvsiu31ell.wor8sp+0<TAG1>Data_To_Be_Extracted</TAG1>jscb akjbfq23jrbfhhhh3
aukwdhaiubf23__fgv<TAG1>Data_To_Be_Extracted</TAG1>e mot ri t ubs rjsa 3221u3r
root@isau02:/data/tmp/testfeld> sed 's/.*<TAG1>\([^>]*\)<\/TAG1>.*/\1/g' infile2
Data_To_Be_Extracted
Data_To_Be_Extracted
Data_To_Be_Extracted
root@isau02:/data/tmp/testfeld>

I thought that is what you wanted. You got the stuff between those two tags. It's not touching the input file, if you mean that.

Last edited by zaxxon; 01-20-2009 at 05:44 AM.. Reason: removed a domain name
# 6  
Old 07-23-2008
Thanks a lot Zaxxon Smilie... it is running fine when I am hard coding the file name but if I am putting the file name like:
Code:
sed 's/.*<TAG1>\([^>]*\)<\/TAG1>.*/\1/g' $fileName

it is returning the entire file.
Note: There are multiple occurences of <TAG1>.

Last edited by Rajat; 07-23-2008 at 01:41 PM..
# 7  
Old 07-23-2008
i guess it's just working with just one particular file for some weird reason ... any suggestions people?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with GREP + CUT - script for automatic update of STEAM GAME AR

Hello, I have a problem with the /etc/rc.d/init.d script to automatically update STEAM GAME ARK. I've converted 3 scripts into one, but something does not work correctly ... The problem is in the file latestavailableupdate.txt / line 36/39. It think the problem is with script, it wrongly... (2 Replies)
Discussion started by: kshishu
2 Replies

2. Shell Programming and Scripting

Cut Lines using grep

Hi, I have a SQL script with "create table" and "alter table" statements and I want to cut all the alter table statements from original file (A) and move it to a different file (B). Can you please me the option. Thanks a lot for your time. (3 Replies)
Discussion started by: bhupinder08
3 Replies

3. Shell Programming and Scripting

grep with cut option??

This is my command-line code in my script, passwd=`grep $passwd $userfile | cut -f2 -d: login_users > retrieve` the cut -f2 -d: login_users > retrieve searches and prints the whole column two. what I need is one of the items in the column two only.. what option can I add to my cut... (2 Replies)
Discussion started by: jenimesh19
2 Replies

4. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

5. Shell Programming and Scripting

Problem in getting data from a loop using grep and cut

The script is following : for each_rec in <file_name> do count=`cut -c -2 ${each_rec} | grep "45"` echo ${count} if ] then amount=`cut -c 24-35 ${each_rec}` echo ${amount} else echo "failed" fi done And the file looks like below : ... (4 Replies)
Discussion started by: mady135
4 Replies

6. Shell Programming and Scripting

!!EMERGENCY!! - GREP/CUT to array

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa 41aaa 42000 43aaa... (5 Replies)
Discussion started by: gc_sw
5 Replies

7. Shell Programming and Scripting

Using grep and cut within awk

Hi My input file looks like as follows: say a.txt "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss... (5 Replies)
Discussion started by: bittoo
5 Replies

8. Shell Programming and Scripting

tail, grep and cut

Hello all, I have some weird problem that kinda baffles me. Say I have the following test file: claudia:~/tmp$ cat testfile.txt This is a test line This is the second test line And yeah, this is the third test line Then say I want to tail the file, grep for the word "third" then... (7 Replies)
Discussion started by: sylaan
7 Replies

9. Shell Programming and Scripting

cut sed grep or other?

Hi Is there a way to cut the last two characters off a word or number given that this word or number can be of varying length? I have tried something like TEST=`echo $OLD | cut -c 1-5` where $OLD is a variable containing a number like 1234567 which gives a result of 12345. This is fine... (4 Replies)
Discussion started by: rleebife
4 Replies

10. Shell Programming and Scripting

grep and cut....

hi, i have a simple question: in hpux 11i; this cmd : cat mailtest |grep sekar >test1 gives the output file with the name test1. i want to remove the line which contains the "sekar" and put the result in the new file.... what is the command for that?.. (2 Replies)
Discussion started by: sekar sundaram
2 Replies
Login or Register to Ask a Question