Counting number of records with string row delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting number of records with string row delimiter
# 1  
Old 10-12-2011
Counting number of records with string row delimiter

HI,

i have a file like this
t.txt
Code:
f1|_f2|_
f1|_f2|_
f1|_f2|_

as if col delimiter is |_ and row delimiter |_\n

trying to count number of records using awk

Code:
$ awk 'BEGIN{FS="|_" ; RS="~~\n"} {n++}END{print n} ' t.txt
7



wondering how can i count this to 3 ?

thx
a

Last edited by Franklin52; 10-12-2011 at 05:48 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 10-12-2011
Quote:
Originally Posted by aksforum
..
i have a file like this
t.txt

f1|_f2|_
f1|_f2|_
f1|_f2|_

as if col delimiter is |_ and row delimiter |_\n

trying to count number of records using awk

...wondering how can i count this to 3 ?
...
Code:
$
$ cat t.txt
f1|_f2|_
f1|_f2|_
f1|_f2|_
$
$ awk 'BEGIN {FS="|_"} {print "Record Number: ", NR, "No. of fields = ", NF}' t.txt
Record Number:  1 No. of fields =  3
Record Number:  2 No. of fields =  3
Record Number:  3 No. of fields =  3
$
$

tyler_durden
# 3  
Old 10-12-2011
Quote:
Originally Posted by aksforum
trying to count number of records using awk
Code:
awk 'END{print NR}' file

or:
Code:
awk '{n++}END{print n}' file

# 4  
Old 10-12-2011
okay, i confused it.. here is the text file
Code:
f1|_f2|_f3
|_f4~~
f1|_f2|_f3
|_f4~~
f1|_f2|_f3
|_f4~~

you can see that field f3 has a new line character in it.. but i want ~~\n as row delimiter adn so it should count to 3.
Code:
awk 'BEGIN{FS="|_" ; RS="~~\n"} {print NF, n++}END{print n} ' t.txt
5 0
0 1
6 2
0 3
6 4
0 5
2 6
7


somehow awk doesn't take multiple character as field or row delimiters.? how do i that?

thx

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-12-2011 at 06:10 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 10-12-2011
awk definitely supports multiple characters as record separators. I tested with your script and your data, it even works with a crummy buxybox awk version.

I think your data's not what you think it is. Did you edit this text file in windows?
# 6  
Old 10-12-2011
Try:
Code:
awk -F"|_" '{print NF, n}/~~$/{n++}END{print n} ' t.txt

# 7  
Old 10-12-2011
here is the copy pasted data from vi editor
f1|_f2|_f3
|_f4~~
f1|_f2|_f3
|_f4~~
f1|_f2|_f3
|_f4~~

don't know a copy/paste going to add anything extra (like \r )

thx
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

I want count of number of records to be printed on each row.

we want the count of number of records to be printed on each row. For Ex: if there are 5 records on one unique id , the count "5'' should be printed on each record in other column. Please help for this. I am using unix & Cygwin. Below are sample records: KCZ0650473... (2 Replies)
Discussion started by: ElijaRajesh
2 Replies

2. Shell Programming and Scripting

Counting number of single quotes in a string

i need to be able to count the number of single quotes ' in the entire string below: "description":"DevOps- Test VM's, System Admins Test VM's ", awk can most likely do this, but here's my attempt using egrep: echo "${STRING}" | egrep -wc '"'"\'"'"' or echo "${STRING}" | egrep -wc... (11 Replies)
Discussion started by: SkySmart
11 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Help- counting delimiter in a huge file and split data into 2 files

I’m new to Linux script and not sure how to filter out bad records from huge flat files (over 1.3GB each). The delimiter is a semi colon “;” Here is the sample of 5 lines in the file: Name1;phone1;address1;city1;state1;zipcode1 Name2;phone2;address2;city2;state2;zipcode2;comment... (7 Replies)
Discussion started by: lv99
7 Replies

5. Shell Programming and Scripting

Help me in counting records from file

Hi, Please help me in counting the below records(1st field) from samplefile: Expected output: Count Descr ------------------------------------------- 7 Mean manager 14 ... (7 Replies)
Discussion started by: prashant43
7 Replies

6. Shell Programming and Scripting

Counting records with AWK

I've been working with an awk script and I'm wondeing id it's possible to count records in a file which DO NOT contain, in this instance fields 12 and 13. With the one script I am wanting to display the count for the records WITH fields 12 and 13 and a seperate count of records WITHOUT fields... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

7. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

8. Shell Programming and Scripting

Help required for counting delimiter

Hi All, I have a delimited file.Sometime it happens that delimiter get missed between 2 fields,so i need to count the number of delimiter present at each line. exam file.txt a|b|c|d e|f|a cc|dd so output should be 1 3 2 2 3 1 Thanks (2 Replies)
Discussion started by: ravi.sadani19
2 Replies

9. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question