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
# 15  
Old 10-27-2015
Quote:
Originally Posted by MOHANP12
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
Note that your code above has an unmatched backquote at the end of the awk command. And, the awk command and the assignment to the shell variable delim_flag are completely independent and do not affect each other in any way.

Furthermore, the awk command I suggested (modified to look for 29 field delimiters instead of 3:
Code:
awk -F '[~][|][*]' 'NF!=30{x=1;exit}END{print x+0}' file

produces the same results as your awk command, but runs faster if any lines are found in your input file that do not contain the desired number of field delimiters. (Your code reads every line in the input file; my suggested code stops reading the input file as soon as it finds a line that does not contain the specified number of fields.)

If you are trying to set a shell variable to a value to indicate whether or not an error (a line with the wrong number of delimiters) was found, the way to do that would be something like:
Code:
delim_flag="$(awk -F '[~][|][*]' 'NF!=30{x=1;exit}END{print x+0}' file)"

# 16  
Old 11-26-2015
MY sample data
Code:
ABC|edgf|T1J333|201211304
ABC|edgf|T1J333|201211303
ABC|edgf|T1J333|201211302
ABC|edgf|T1J333|20121131
TRL00004

new requirement in which Trailer is add in file
Code:
delim_flag=0
	delim_flag_val=`awk -F "|" 'BEGIN{delim_flag=0} NF != 38 {delim_flag=1} END{print dlim_flag}' "FILENAME"`

please suggest how i ignore in same command

Last edited by Don Cragun; 11-26-2015 at 02:36 PM.. Reason: Fix existing CODE tags; add missing CODE tags.
# 17  
Old 11-26-2015
Please use code tags as required by forum rules!

Actually, regardless of the trailer being present or not, your code applied to the sample data will yield a "1" printed, as there won't be any lines with 38 fields.

And, in order to present a half way reasonable suggestion to solve your problem, additional info is needed:
- is the trailer always one line?
- is the trailer always one field?
# 18  
Old 11-26-2015
MY sample data
Code:
ABC|edgf|T1J333|201211304
ABC|edgf|T1J333|201211303
ABC|edgf|T1J333|201211302
ABC|edgf|T1J333|20121131
TRL00004

new requirement in which Trailer is add in file
Code:
delim_flag=0
delim_flag_val=`awk -F "|" 'BEGIN{delim_flag=0} NF != 4 {delim_flag=1} END{print dlim_flag}' "FILENAME"`

please suggest how i ignore trailer in file . trailer always one line and one field and start with TRL

Last edited by Don Cragun; 11-26-2015 at 02:40 PM.. Reason: Fix existing CODE tags; add missing CODE tags.
# 19  
Old 11-26-2015
Please use code tags as required by forum rules!

---------- Post updated at 15:39 ---------- Previous update was at 15:36 ----------

A few options:
Code:
awk -F "|" 'BEGIN {LAST = 4} LAST != 4 {delim_flag=1; exit} {LAST = NF} END{print delim_flag+0}' file
0
awk -F "|" '/^TRL/ {next} NF != 4 {delim_flag=1; exit} END{print delim_flag+0}' file
0

# 20  
Old 11-26-2015
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

Please let me know how to use code tag
# 21  
Old 11-26-2015
Use e.g. the code tag button. There's a hint/description right above the editor window.
This User Gave Thanks to RudiC 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