Showing posts with label CommandLine. Show all posts
Showing posts with label CommandLine. Show all posts

Friday, September 22, 2017

193. Valid Phone Numbers

https://leetcode.com/problems/valid-phone-numbers/description/
^   start with
$   end with

basic regex
(   {   |    are regular characters matching ( { |
\(  \{  \| ...  are special operations
grep '^\([0-9]\{3\}-\|([0-9]\{3\}) \)[0-9]\{3\}-[0-9]\{4\}$' file.txt

extended regex
(   {   |    are special operations
\(  \{  \| ...  are escape characters matching ( { |
grep -E '^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$' file.txt

Tuesday, September 12, 2017

195. Tenth Line

https://leetcode.com/problems/tenth-line/description/
#
cnt=0
while read line && [ $cnt -le 10 ];
do
  let 'cnt = cnt + 1'
  if [ $cnt -eq 10 ]; then
    echo $line
    exit 0
  fi
done < file.txt

#use AWK
awk '{if(NR==10) print $0}' file.txt
awk 'FNR == 10 {print }'  file.txt
awk 'NR == 10' file.txt

#use sed
sed -n 10p file.txt

#use tail
tail -n+10 file.txt|head -1