0

enter image description here

I have an input delimiter file. The actual number of records in the file is 4. But due to carriage-returns in the values, now the total count is 8. I just want to do a count of rows based on the first column values.

Answer Should be: 4.

I tried using these commands but none are working:

grep -Eo '[0-9]+|' filename | sort -u | wc -l

awk -F '|' '{sub(/[^[:digit:]]+/, "", $1); a[$1]} END{for (z in a) ++i; print i}' filename

awk -F '|' '{sub(/[^[:digit:]]+/, "", $1); PRINT[$1]} END{for (z in a) ++i; print i}' filename

wc -l filename | sed 's/ *\([0-9]* \).*/\1/'
  • Welcome to U&L. We appreciate if you don't waste our time by including chit-chat. Please read at the least the help→tour to understand about chit-chat and other question unrelated stuff. We also prefer cut-and-pasted text over screenshots (makes it possible to test answer code on real values). – Anthon Aug 27 '15 at 17:50
  • So how are you supposed to tell the end of a record vs the end of a line? If the file is really only 8 records long, modify the file to get rid of extra line endings. – Robert Jacobs Aug 27 '15 at 18:23
  • are you saying that the rule for a line contributing to your count is that it must start with some number of digits? That's a fairly generic rule that should be possible to capture with grep. – Centimane Aug 27 '15 at 18:30
2

This was closest:

grep -Eo '[0-9]+|' filename | sort -u | wc -l

but it missed the goal by

  • not anchoring the match to the beginning of the line
  • unnecessarily sorting / removing duplicates from the data

To anchor the expression, put "^" at the beginning of the pattern, and escape the "|" (since it is a meta character):

grep -Eo '^[0-9]+\|' filename | sort -u | wc -l

Next - discard the sort -u. The grep ignores the continuation lines, and it appears possible that some "duplicates" can be removed that are not really duplicates if the extra information were used.

Finally, discard the wc -l: POSIX grep has a -c option which tells grep to print the count of matches. Drop the -o option (it is not needed). So all you need is

grep -Ec '^[0-9]+\|' filename
0

this may work

grep -c ^the desired string filename
wc -l thefile
  • Thanks for reply. Here i am not looking the counts based on string. Could you please elaborate little more...:) – Karthik Aug 27 '15 at 17:52
  • do you need some quotes here? – ctrl-alt-delor Aug 27 '15 at 23:00
  • @Karthik what are you looking for, amend you question, to be clearer. – ctrl-alt-delor Aug 27 '15 at 23:01
  • Hi, I am doing count validation between Source pipe delimiter file(Total number of Actual records) and reference file(Rowcount value given). So for some of the rows, carriage return values are populated as new line feeds and which gives the wrong count of rows in source file. The first column in file unique integer once. I dont want to delete the carriage values from file because after the validation, i need to load this file to a table. So without deleting carriage values. I need to get actual number of records in source file based on first column. I used below grep '[0-9]|' filename |wc -l – Karthik Aug 28 '15 at 18:01
  • Command used is: grep '[0-9]|' filename |wc -l.. But still some of the line feeds are also taken in to count which i dont want. I want the check that if there are any thing apart form integer in first column then filter integer. Really Appreciate your help. Thanks in advance! – Karthik Aug 28 '15 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.