Find number begins with 24

Hi Team,

i need to list only those number from the input file which begin with 24

input file is 
2412
2413
2456
2134
2134
2244
2526
expected output i want is 
2412
2413
2456

Hi, have you tried to use grep ?

It is always worth while using the interactive interpreter first:-
This is a simple longhand starter that requires nothing but a bash terminal.
OSX 10.12.4, default bash terminal.

Last login: Thu Apr 27 10:53:04 on ttys000
AMIGA:amiga~> echo '2412
> 2413
> 2456
> 2134
> 2134
> 2244
> 2526' > /tmp/24.txt
AMIGA:amiga~> while read -r line; do if [ "${line:0:2}" == "24" ]; then echo "${line}"; fi; done < /tmp/24.txt
2412
2413
2456
AMIGA:amiga~> _

@wisecracker, when using the shell's read command rather than a line processing external utility to process all the characters on the line, it is best to use IFS= to prevent the interpretation of whitespace (otherwise it would also match lines starting with 24 ). Likewise when printing the result, it is best to use printf to avoid interpretation by the non-standard echo command, that you previously avoided with the -r option.

So:

while IFS= read -r line
do
  ....
  printf "%s\n" "$line"
done < file

--
Note:
if

[ "${line:0:2}" == "24" ]

should syntactically be either

[ "${line:0:2}" = 24 ]

or

[[ ${line:0:2} == 24 ]]

since ${var:pos:num} is bash/ksh93/zsh, one might also use:

[[ $line == 24* ]]

Hi

sorry, but I am not able to understand you suggestion

the number is present on the 18th field in file.

I tried below syntax but it is giving correct output on few nodes and on few nodes it is giving incorrect value

 
 cat v.txt | cut -d',' -f18 | grep -i ^24 | wc -l
 

Try:

awk -F, '$18~/^24/' file

If that is not working, then could you please post a representative sample of your input file and the desired output..

awk -F ','   '$18 ~ /^24/ { print $18 }' inputfile > outputfile

This prints 18th field where that field number - 18 - starts with a 24.

Your grep command does not match what you actually need, and neglected to mention the field requirement. It helps both you and the people helping you to give enough information to start with. The more you assume the less we can help you.

wc -l

counts lines it does not print field #18. If you want find how many lines match then pipe the output of the above awk into wc -l