Grep char count & pipe to sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep char count & pipe to sed command
# 1  
Old 05-06-2010
Grep char count & pipe to sed command

Hi
I am having a 'grep' headache
Here is the contents of my file:

(PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1)))

I would like to count out how many times 'PBZ' occurs and then place that number in the line above

3
(PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1)))


This is how i am doing so far on my code
Code:
grep 'PBZ' FileIn | wc -c | while read i;
do sed '1i\ 
$i' FileIn > Fileout
done

This is what i get for my Fileout
$i(PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1)))


I know that there are three main problems here

1) grep 'PBZ' FileIn | wc -c = 70 .... it is counting every character instead of just counting all the 'PBZ'
2) When i do my while read i command, it does not carry the count over to my sed command thus my $i was not changed in my code
3) The $i is being placed before, but on the same line as my string, i would like it to be on a separate line above my string.

If anyone has any suggestions or tips on how i could correct this it would be very much appriciated.Smilie
# 2  
Old 05-06-2010
Code:
grep -c 'PBZ' FileIn | while read i;
do sed '1i\ 
$i' FileIn > Fileout
done

# 3  
Old 05-06-2010
Grep char count & pipe to sed command

Nope, thats not it Smilie

grep -c 'PBZ' FileIn gives me a count of 1, and unfortunatly its still not reading through to the sed command.

welcome to my hell!
# 4  
Old 05-06-2010
Quote:
Originally Posted by cavanac2
welcome to my hell!
I rather go to heaven Smilie...try this:
Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","") RS $0)}1' file

# 5  
Old 05-06-2010
testing

Now be aware, my understanding of awk is limited, so im sorry if i offend with my bad code

I tested
Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","") RS $0)}1' FileIn

however it gave me this
3
(,CP,(((ME,,BtM),ON),((ME,((,DG),(CW9,PG11))),CW9,TS2,RT1)))

it effectivly deleted my PBZ from my files. I tried to reslove this by deleting gsub("PBZ","") but then of course it didnt work atall

how would i go about fixing this without just saying substitute PBZ with PBZ??

Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","PBZ") RS $0)}1' FileIn

# 6  
Old 05-06-2010
Quote:
Originally Posted by cavanac2
Now be aware, my understanding of awk is limited, so im sorry if i offend with my bad code

I tested
Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","") RS $0)}1' FileIn

however it gave me this
3
(,CP,(((ME,,BtM),ON),((ME,((,DG),(CW9,PG11))),CW9,TS2,RT1)))

it effectivly deleted my PBZ from my files. I tried to reslove this by deleting gsub("PBZ","") but then of course it didnt work atall

how would i go about fixing this without just saying substitute PBZ with PBZ??

Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","PBZ") RS $0)}1' FileIn

My fault, I forgot the variable s in the substitution, try this:
Code:
awk '/PBZ/{s=$0;$0=(gsub("PBZ","",s) RS $0)}1' file

# 7  
Old 05-06-2010
How about ...

Code:
grep -o 'PBZ' in.file | wc -l | while read i
do
  sed "1i\
  $i" in.file >> out.file
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

2. Shell Programming and Scripting

Sed: -e expression #1, char 2: extra characters after command

Greetings.. getting the error while execution of the script, correct where i am missing #!/bin/bash DATE=`date +%Y-%m-%d:::%H:%M` HOSTNAME=`hostname` TXT="/log/temp.txt" LOGPATH="/log1/commanlogs/" IP=`/sbin/ifconfig | grep -i inet| head -n1| awk '{print $2}'| awk -F : '{print $2}'`... (7 Replies)
Discussion started by: manju98458
7 Replies

3. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

4. Shell Programming and Scripting

applescript & grep - sed command

I'm new using Unix commands in applescript. The following script you choose different folders with PDfs, get file count of PDfs on chosen folders, & write the results in text file. set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with... (0 Replies)
Discussion started by: nellbern
0 Replies

5. UNIX for Dummies Questions & Answers

Help with grep & count in ksh

Hello, I have input file with queue names No.esprd.Queue|No.esdev.Queue|||n|120|No_User||No_User| No.esdev.Queue|No.esdev.Queue|||n|120|No_User||No_User| I have to check if the input file contains word "esprd" and the number of times it occurs. I will have to do further... (22 Replies)
Discussion started by: Green_Star
22 Replies

6. Shell Programming and Scripting

sed: -e expression #1, char 21: unterminated `s' command

I have read many threads, but I still didn't find the right answer. May be i didn't find the right thread, though are so many threads for the same question. Basically the situation is - find date in a file and replace it with another date. (its not homework, its part of lot of a big processing,... (10 Replies)
Discussion started by: avinthm
10 Replies

7. Shell Programming and Scripting

Help Needed with grep & sed

On one of my servers, it appears that a bunch of html files got the following code added to it... I was going to try to remove this line using grep & sed... as sample grep -lr -e 'apples' *.html | xargs sed -i 's/apples/oranges/g' I can get the grep portion to work... grep "<script... (7 Replies)
Discussion started by: djlane
7 Replies

8. Shell Programming and Scripting

pipe output of grep to sed?

Is there a way I can do this: search for text and replace line containing matched text with a different line? For example: "I want to replace text" I want to search for replace and then change the line to I am perplexed. Hope that makes sense. Thanks in advance. (4 Replies)
Discussion started by: arsh
4 Replies

9. UNIX for Dummies Questions & Answers

Pipe results of Grep Command to LS Comand

I'm using the command grep -l XYZ to get a list of files containing the string XYZ. Then I using the comand ls -l ABC to get the create date timestamp of the each file. I've tried combining the comands using the pipe command, grep -l XYZ | ls -l, but its not working. What am I doing wrong? (3 Replies)
Discussion started by: jhtate
3 Replies

10. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies
Login or Register to Ask a Question