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.
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.
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.
The code you provide is given same error . There is issue with *
[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
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.
[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
Could you please try following and let me know if this helps.
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 fields NF>4 you could use it as per your need too. Let us know how it goes.
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
[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
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:
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
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.
awk -F '[~][|]
[*]' '{print NF}' Input_file
Output will be as follows.
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.
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.
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:
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:
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: