How to add number of fields throughout file together?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to add number of fields throughout file together?
# 1  
Old 11-05-2019
How to add number of fields throughout file together?

So, I have three problems that cover this subject.

First one asks me to find the number of fields in the file that contain the substring "he". I found the number of fields, but the problem I have is that they are displaying by each record. I want to add all of the records' fields together.
With this same text document:

"The quick brown fox jumped over the lazy dog's back.
The rain in Spain falls mainly on the plain.
The rain in Spain also falls on the mountains."
My code looks like this:

Code:
{print "Number of fields in record containing 'he': " index($0, "he")}

It is giving me this result:

"Number of fields in record containing 'he': 2
Number of fields in record containing 'he': 2
Number of fields in record containing 'he': 2"
What I want it to do is print

"Number of fields in file containing 'he': 6"
How would I accomplish that?

These are the two other problems, of a very similar type: I want to find the number of times the letter n appears in the text file, but I do not know how to add all the records' results together. My current code looks like this:

Code:
{print "Number of 'n' in record: " gsub(/n/, "")}

This gives me the result of:

"Number of 'n' in record: 1
Number of 'n' in record: 6
Number of 'n' in record: 6"
Instead, I need it to say

"Number of 'n' in file: 13"
The last problem is essentially the same, but instead of 'n', I need to look for the number of 'la' in the file.
# 2  
Old 11-05-2019
Welcome,
This looks like home work to me, so unless you can justify, the thread is closed, as we have a forum dedicated to home/classwork with its own rules you have to comply with
# 3  
Old 11-06-2019
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. UNIX for Beginners Questions & Answers

How to add fields to different file?

hi, would you help me? i have file total.csv "name","id","gender" "a,"e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" and file count.csv 3 i want to add file count to file total every row name,id,gender,total_match "a,"e23","f","3" "b,"34d","g","3" "c","45f","f","3" ... (4 Replies)
Discussion started by: kivale
4 Replies

3. Shell Programming and Scripting

awk processing of variable number of fields data file

Hy! I need to post-process some data files which have variable (and periodic) number of fields. For example, I need to square (data -> data*data) the folowing data file: -5.34281E-28 -3.69822E-29 8.19128E-29 9.55444E-29 8.16494E-29 6.23125E-29 4.42106E-29 2.94592E-29 1.84841E-29 ... (5 Replies)
Discussion started by: radudownload
5 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Help correcting file with differing number of fields

Hi all, I have a tab separated file, and one of the fields is sub-delimited by colon. The problem is there can be zero to 4 colons within this field. When I try to change colons to tabs the result is a file with a differing number of fields. I want to go from: a:b:c:d:e a:b:c a:b:c:d:e a... (4 Replies)
Discussion started by: torchij
4 Replies

5. Shell Programming and Scripting

Printing Number of Fields with the line number

Hi, How to print the number of fields in each record with the line number? Lets saw I have 3212|shipped|received| 3213|shipped|undelivered| 3214|shipped|received|delivered I tried the code awk -F '|' '{print NF}' This gives me ouput as 3 3 4 (5 Replies)
Discussion started by: machomaddy
5 Replies

6. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

7. Shell Programming and Scripting

separate the file according to the number of fields

I have a file which is delimetered by ',' i need to filter out a file with respect to the number of fileds in each line. a,s,d,f,g,h,j,k,l 1,2,3,3,4,5,6,7,6 a,2,3 4,5,6,7 in this i neeed to filter out the lines with 8 column to another file and rest to another file. so ... (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

8. UNIX for Advanced & Expert Users

Problem while counting number of fields in TAB delimited file

I'm facing a strange problem, please help me out. Here we go. I want to count number of fields in particular file. filename and delimiter character will be passed through parameter. On command prompt if i type following i get 27 as output (which is correct) cat customer.dat | head -1 | awk... (12 Replies)
Discussion started by: vikanna
12 Replies

9. Shell Programming and Scripting

number of fields in a text file as a variable - perl

I am looking for perl code to get following o/p. If a line has more than 7 fields then value in field 7 onwards is BHA_GRP1, BHA_GRP2, BHA_GRP3, BHA_GRP4 etc. Here is example of what I am trying to achieve. INPUT File: VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP1 BHA_GRP2 BHA_GRP3...... 2... (1 Reply)
Discussion started by: dynamax
1 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question