count and number instances of a character in sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count and number instances of a character in sed or awk
# 8  
Old 11-20-2011
Quote:
Originally Posted by maouinin
Thanks for all replies. This seems to work beautifully now:

Sorry for the lack of tags and poor formatting before. This is my first thread/post on your site.
Glad you got it figured out. One of the things that makes this forum great, IMHO, is that everybody jumps in to help. I don't know why I used gsub() rather than a straight assignment this morning, and didn't catch the problem that it would cause until after your post, but the others chimed in with solutions/corrections before I got back to see and fix my code.

As for using code tags which make it easier to read data and programmes, you might have a read over this:
The UNIX and Linux Forums - BB Code List

Cheers
# 9  
Old 11-24-2011
Quote:
Originally Posted by vgersh99
I think you want something like this:
Code:
nawk '/=/{for(i=1;i<=NF;i++) if ($i ~/^=/) $i=("_______[" ++count "]" FS substr($i,2))}1' myFile

This also works brilliantly.

Given:
Code:
I am =honored to be with you =today at your commencement 
=from =one =of =the =finest universities in the world. I 
never =graduated from =college. Truth be told, this is the
=closest I've =ever =gotten to a =college =graduation.
Today I want to tell you three stories =from =my =life.
That's it. =No =big =deal. Just three stories.

this code
Code:
nawk '/=/{for(i=1;i<=NF;i++) if ($i ~/^=/) $i=("[" ++count "]__________" FS substr($i,2))}1' filename

gives:

Code:
I am [1]__________ honored to be with you [2]__________ today
 at your commencement [3]__________ from [4]__________ one
 [5]__________ of [6]__________ the [7]__________ finest universities 
in the world. I never [8]__________ graduated from [9]__________ college.
 Truth be told, this is the [10]__________ closest I've [11]__________ ever
 [12]__________ gotten to a [13]__________ college [14]__________
 graduation. Today I want to tell you three stories [15]__________ from
 [16]__________ my [17]__________ life. That's it. [18]__________ No
 [19]__________ big [20]__________ deal. Just three stories.

Thanks so much.

BTW, what is the purpose of the 1 after the close braces at the end?
Code:
nawk '/=/{for(i=1;i<=NF;i++) if ($i ~/^=/) $i=("[" ++count "]__________" FS substr($i,2))}1'

---------- Post updated at 03:47 PM ---------- Previous update was at 02:59 PM ----------

The successful solution to the original question of how to replace a string beginning with an equals sign with a running question number and a blank space for students to write answers -- "=college" becomes "(8)__________", for example) has led me to a secondary question.:

How might I replace each letter of the word with two underscore and a space -- "=college." becomes "(8)__ __ __ __ __ __ __." Or for even weaker students, "=college." becomes "(8)c __ __ __ __ __ __."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

3. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

4. Shell Programming and Scripting

count the number of instances in 2 columns using awk

Input A.1 Q.1 A.1 Q.2 A.1 Q.3 A.2 Q.4 Explanation: Final Output A.1 Q.1 s1 t1 A.1 Q.2 s1 t2 A.1 Q.3 s1 t3 A.2 Q.4 s5 t5 ---------- Post updated 09-28-12 at 03:38 AM ---------- Previous update was 09-27-12 at 09:10 AM ---------- Hi Guys, I was able to do until... (11 Replies)
Discussion started by: quincyjones
11 Replies

5. Shell Programming and Scripting

Count number of character occurence but not from quotation marks

I have the following string: 31-01-2012, 09:42:37;OK;94727132638;"Mozilla/5.0 (Linux; U; Android 2.2.1)";3G;WAP;I need a script which is counting the occurrence of semicolons ( ; ) but exclude the ones from the quotation marks. In the string given as example there are 8 semicolons but the script... (3 Replies)
Discussion started by: calinlicj
3 Replies

6. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

7. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

8. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

9. Shell Programming and Scripting

hw can i count the number of character in a file by perl

i want to count the number of character contained in afile using perl cript help me out (1 Reply)
Discussion started by: trupti_rinku
1 Replies

10. UNIX for Dummies Questions & Answers

Sed character number specification

I have the following sed command: sed '/7361105/s/^\(..............................................................................................................................................................................\)....\(.*\)/\16776\2/' arch.txt > arch.tmp && mv arch.tmp arch.txt ... (4 Replies)
Discussion started by: mvalonso
4 Replies
Login or Register to Ask a Question