grep and cut problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep and cut problem
# 8  
Old 07-24-2008
I guess I live in another timezone and when work is over, I go home and usually don't look into here anymore. That's why I didn't answer that fast Smilie Can you post the pm question here please? I will describe the syntax soon as edit in this post here.

EDIT:

Code:
sed 's/.*<TAG1>\([^>]*\)<\/TAG1>.*/\1/g'

s = substitution
/ = start of the pattern
. = any character
* = zero or as many of the former character
\( = escaped starting bracket of the group I want to extract
[ = starting squared bracket of a group of characters
^ = inside the squared bracket means "not/none" of the following, in this case as long as no > shows up
] = ending the group
* = zero or as many of the former character, in this case as many as it is no >
\) = escaped bracket to end the group definition
\/ = just escaping the slash of that end tag
.* = zero or as many of any character (you know that by know already Smilie )
/ = here ends the pattern I want to find and starts that, through what I want to substitute
\1 = print out the 1st group I defined inside the \( \) escaped curled brackets
/ = end of the substitution input
g = globally, do it on the whole line of input


Best search the web for sites that explain regular expressions (reg exp) or get that nice awk&sed book from O'Reilly which is worth it, the small reference book and/or the "bigger" one.

Last edited by zaxxon; 07-24-2008 at 01:48 AM..
# 9  
Old 07-29-2008
Thanks a lot Zaxxon ... that post really helped me a lot ... however if the file is a huge file then it is taking a lot of time to get executed.
I'll tell you exactly what I am tryin to do:
I need to get the value in between the tags <TAG></TAG1>.
Then I need to count the number of some other tag. So the situation here is:
sed 's/.*<L:TAG>\([^>]*\)<\/L:TAG>.*/\1/' $hugefile
So this is reading the whole file in spite of removin the g at the end!!!
Then I have got this line to count the number of the other tags which is again reading the entire file!
otherTagCount=`egrep -hc -e $searchString $line | awk '{sum+=$1};END{print sum}'`
I have got the above line 4 times to count different tags.
So it is taking double time to execute the script.
Thanks in advance!!!
# 10  
Old 07-29-2008
sed is only processing the file one line at a time. It does not read across newline boundaries.

If the tag structure is complex then perhaps it would be wiser to use XSLT or something to get precisely what you want.

Combining the grep | awks into a single awk script sounds like the obvious thing to optimize. Something like

Code:
sed whatever $hugefile | awk '/searchstring1/ { ++count1 }
  /searchstring2/ { ++count2 }
  /searchstring3/ { ++count3 }
END { print "count1=" count1 " count2=" count2 " count3=" count3 }'

perhaps?
 
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