Count delimiter(~|*) each row in a file and return 1 or 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count delimiter(~|*) each row in a file and return 1 or 0
# 8  
Old 10-26-2015
Quote:
Originally Posted by MOHANP12
... ... ...
Already try all condition FS="~|\*" \ for specical chrcter
As I said before, try:
FS='~\|\*'
You need to escape both the pipe symbol and the asterisk!

Actually, depending on where you put the above code you might need different quotes and double escapes. I suggested:
Code:
awk -F '~\|\*' ...

which should work. You could also use:
Code:
awk -v FS='~\|\*' ...

Inside double quotes depending on where you place the statement:
Code:
FS="~\|\*"

you might need:
Code:
FS="~\\|\\*"

instead.

Last edited by Don Cragun; 10-26-2015 at 06:29 AM.. Reason: Add qualifier...
# 9  
Old 10-26-2015
Hello MOHANP12,

Could you please try following and let me know if this helps.
Code:
awk 'BEGIN{gsub("~|*",FS,$0);print "Line Number \t status"} {if(NF>4){print NR OFS OFS 1} else {print NR OFS OFS 0}}' OFS="\t" Input_file

Considering your columns doesn't have a space in their values. Also here I am taking Number of fieldsNF>4 you could use it as per your need too. Let us know how it goes.

Thanks,
R. Singh
# 10  
Old 10-26-2015
Quote:
Originally Posted by Don Cragun
The asterisk has special meaning in an ERE too.

Assuming you aren't on a Solaris/SunOS system, try:
Code:
awk -F '~\|\*' 'NF != 4 {delim_flag=1; exit} END{printf("%d\n", delim_flag}' "filename.txt"


Thanks in advance but these all condition are already try not working .My OS is AIX.

---------- Post updated at 06:45 AM ---------- Previous update was at 05:21 AM ----------

Hi RabinderSIngh13

I try your code also but got same error
Code:
[ERROR]
+ delim_flag=0
+ + awk BEGIN{gsub("~|*",FS,$0);print "Line Number \t status"} {if(NF!=30){print NR OFS OFS 1} else {print NR OFS OFS 0}} OFS=\t /tmps/data/TPMS_DEV5/TPMS/FM_HOME/inbound/remit/cerf/TGT_OUTBND_20151012_122417DUP.txt
awk: 0602-521 There is a regular expression error.
        ?*+ not preceded by valid expression.

 The source line number is 1.
 The error context is
                 >>> BEGIN{gsub("~|*",FS,$0) <<<
DELFLADF=
+ echo


Last edited by Don Cragun; 10-26-2015 at 02:21 PM.. Reason: Add CODE tags.
# 11  
Old 10-26-2015
Let me try one more time...

It isn't clear to me whether you are trying to print a single value indicating whether or not any line in the input file does not have exactly 3 field delimiters (0 if every input line has three delimiters; otherwise, 1); or you are trying to print an indication for each line in the input file (line # and 0 if that line has three delimiters; otherwise line # and 1). For the former, try:
Code:
awk -F '[~][|][*]' 'NF!=4{x=1;exit}END{print x+0}' file

and for the latter, try:
Code:
awk -F '[~][|][*]' '{print NR, (NF!=4)}' file

This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 10-27-2015
Hi Don Cragun

Thanks

Code:
delim_flag=0
awk -F '[~][|][*]' 'BEGIN{delim_flag=0} NF != 30 {delim_flag=1} END{print delim_flag}' "filname"`


This code is working fine. This code test that file contain delimiter as ~|* if any character is missing in any line example ~| or ~* or |* then return 1 or 0

Thanks man you save my day
# 13  
Old 10-27-2015
Hi Ravinder,
In code
Code:
awk -F"~|*" 'BEGIN{print "Line Number \t status"} {if(NF>4){print NR OFS OFS 1} else {print NR OFS OFS 0}}' OFS="\t"  Input_file

in the highlighted part , for the first record , condition would be like this
Code:
(1>4)

, that will hold false and should print the else part. Kindly explain
# 14  
Old 10-27-2015
Hello looney,

There are 2 points here.
1st: When you take value of variable named NF it will give it's final value as follows. Like how many fields are there in a line. So condition which you mentioned it wouldn't work like that, it will not compare it field by field, rather than that it will take total number of field's value. Now if you want to print the number of fields value per line then following may help you in same.
Code:
awk -F '[~][|][*]' '{print NF}' Input_file

Output will be as follows.
Code:
4
4
4
4

Which means each line has 4 fields, thanks to Don to give this field separator -F '[~][|][*]' that's the correct one.

2nd: If you need to get the each field with it's respective value then following may help you.
Code:
awk -F '[~][|][*]' '{for(i=1;i<=NF;i++){print i OFS $i}}'  Input_file

Then you can see the field number and their value as follows here.
Code:
1 ABC
2 edgf
3 T1J333
4 20121130
1 ABC
2 sdaf
3 T1J333
4 20121130
1 ABC
2 fsdg
3 T1J333
4 20121130
1 ABC
2 dfsg
3 T1J333
4 20121130

So we can see here above each field with their respective field positions.
Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-27-2015 at 02:12 AM.. Reason: Added some more clarifications in post
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Row Count in .csv file

Hi, I have to find the count of rows starting with "E," in given a.csv file . Sample Data File. E,2333AED,A,MC3,25,31-MAY-18 E,2333AED,A,MC3,25,31-MAY-18 CYMC3 25AED 0000 E,2333CZK,A,MC3,25,31-MAY-18 CYMC3 25CZK 0000 E,2333EUR,A,MC3,25,31-MAY-18... (3 Replies)
Discussion started by: Prabhakar Y
3 Replies

2. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

3. Shell Programming and Scripting

Split files based on row delimiter count

I have a huge file (around 4-5 GB containing 20 million rows) which has text like: <EOFD>11<EOFD>22<EORD>2<EOFD>2222<EOFD>3333<EORD>3<EOFD>44<EOFD>55<EORD>66<EOFD>888<EOFD>9999<EORD> Actually above is an extracted file from a Sql Server with each field delimited by <EOFD> and each row ends... (8 Replies)
Discussion started by: amvip
8 Replies

4. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

5. UNIX for Dummies Questions & Answers

Count on grep for more than two values in a row of fixed length file

I want to get count on number of records in a few folders by running grep command for more than two columns in a row of fixed length file. suppose if i have a fixed length file has 5 columns and I want to see the record counts for country =can and province = bc and time stamp <= 12 feb 2013... (14 Replies)
Discussion started by: princetd001
14 Replies

6. Shell Programming and Scripting

Column to row with delimiter

Hi, i have data in a file f1.txt a b c d and i want to print the above column values in single line with a delimiter, say ',' The output should look like: a,b,c,d I could find rows to columns help online but not vice versa Thanks, -srinivas yelamanchili (4 Replies)
Discussion started by: ysrini
4 Replies

7. Shell Programming and Scripting

Count the delimeter from a file and delete the row if delimeter count doesnt match.

I have a file containing about 5 million rows, in the file there are some records which has extra delimiter at random position. (we dont know the positions), now we have to Count the delimeter from each row and if the count of delimeter is not matching then I want to delete those rows from the... (5 Replies)
Discussion started by: Akumar1
5 Replies

8. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

row count of 60 files in one file

hi all plz some unix guy help me in this i have 60 files which will have some records i want to find the total number of records in all the 60 files like file1 has 60 and file2 has 70 record i want to sum all these and find total row count in 60 files (5 Replies)
Discussion started by: er_zeeshan05
5 Replies

10. Shell Programming and Scripting

removing a delimiter at the start of row

I Have this code while do column1=":`cat /home/test_inter.txt|head -${iCount1}|tail -1|cut -d "," -f2`" columnA=$columnA$column1 iCount1=`expr ${iCount1} + 1` done echo $columnA (2 Replies)
Discussion started by: nvuradi
2 Replies
Login or Register to Ask a Question