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
# 1  
Old 10-23-2015
Count delimiter(~|*) each row in a file and return 1 or 0

Hi

I want to check delimiter in file. Delimiter in my file is ~|*

sample of file :
Code:
ABC~|*edgf~|*T1J333~|*20121130
ABC~|*sdaf~|*T1J333~|*20121130
ABC~|*fsdg~|*T1J333~|*20121130
ABC~|*dfsg~|*T1J333~|*20121130

in this i want to count number delimiter occur is 4 in each row if count is less then or more then 4 in any row then it return 1 else 0


my command is

Code:
delim_flag=0
awk -F '~|*' 'BEGIN{delim_flag=0} NF != 4 {delim_flag=1} END{print delim_flag}' "filename.txt"

But this command is not working because delimiter is ~|* same command is working when delimiter is | or ~ or ~| but with * there is problem

Last edited by Don Cragun; 10-26-2015 at 05:56 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 10-23-2015
Hello Mohanp12,

Welcome to forums, please use code tags for commands/codes/Inputs which you are using in your posts as per forum rules. Following may help you in same.
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

Output will be as follows.
Code:
Line Number 	 status
1		1
2		1
3		1
4		1

I am putting 1 in output when condition is Number of fields NF>4, while taking delimitor as ~|*. You could change status as per your need too, hope this helps. Enjoy learning.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-23-2015
The problem is the | as it has a special meaning in regexes (alternation).
Try
Code:
awk '{print NR, gsub("~\|*", "&")!=3}' file3
1 0
2 0
3 0
4 0

# 4  
Old 10-26-2015
Rabindra

Thanks in advance


The code you provide is given same error . There is issue with *

Code:
[Error]

awk: 0602-521 There is a regular expression error.
        ?*+ not preceded by valid expression.

The input line number is 1. The file is cerf/TGT_OUTBND_20151012_122417DUP.txt.
 The source line number is 1.
DELFLADF=Line Number     status
+ echo Line Number       status
Line Number      status


Last edited by Don Cragun; 10-26-2015 at 02:20 PM.. Reason: Add CODE tags.
# 5  
Old 10-26-2015
Hello MOHANP12,

You haven't mentioned your OS name, on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk. Let us know if that helps or please do provide us complete details on your OS and errors for same too. I am using bash(LINUX) system and it is working fine for me.


Thanks,
R. Singh
# 6  
Old 10-26-2015
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"

# 7  
Old 10-26-2015
Hi RavinderSingh13

My OS is AIX unix.

Code:
[Error] 

+ + awk BEGIN{FS="~|*"} {if(NF!=30){print NR OFS OFS 1} else {print NR OFS OFS 0}} OFS=\t TGT_OUTBND_20151012_122417DUP.txt
awk: 0602-521 There is a regular expression error.
        ?*+ not preceded by valid expression.

 The input line number is 1. The file is TGT_OUTBND_20151012_122417DUP.txt.
 The source line number is 1.
DELFLADF=
+ echo


Code:
#!/usr/bin/ksh
set -x
#IFS="~|*" "126 124 42"
DELFLADF=`awk 'BEGIN{FS="~|*"} {if(NF!=30){print NR OFS OFS 1} else {print NR OFS OFS 0}}' OFS="\t" "TGT_OUTBND_20151012_122417DUP.txt"`

#DELFLADF=`nawk -F "~|*" 'BEGIN{delim_flag=0} NF != 30 {delim_flag=1} END{print delim_flag}' "TGT_OUTBND_20151012_122417DUP.txt"`
#DELFLADF=`awk '{print NR, gsub("~\|*", "&")!=30}' "TGT_OUTBND_20151012_122417DUP.txt"`
echo "$DELFLADF"


Already try all condition FS="~|\*" \ for specical chrcter

Last edited by Don Cragun; 10-26-2015 at 06:21 AM.. Reason: Add CODE and ICODE tags; fix other CODE tags.
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