[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2680
  • Last Modified:

how to count the row amount of a file and get header and footer with empty row in unix script

There is 1 file FILE.txt , use unix script to get the header and footer rows, count the row amount except header and footer

fdfasdaAggpotrt
1204 500 5 435 46509 65 90650
5409tq09 wtr9w0yt90w009re9tr
vbfdfdbgb002istgj ojoi
(empty row)
(empty row)

i) There maybe is empty row after the footer
ii)check the header 8 position must be "A"
iii)check the footer 10-13 value must equal the row count amount of the file except the header and footer

issue;
head -n 1 FILE.txt  , can get the header
but tail -n 1 FILE.txt can NOT get the correct footer

any advice ?

thanks
0
SayYou_SayMe
Asked:
SayYou_SayMe
  • 3
  • 2
2 Solutions
 
wesly_chenCommented:
footer:
grep -v '^$' FILE.txt | tail -1

Count row (included header and footer)
grep -v '^$' FILE.txt  | wc  -l
   The number subtract  2 will be the number you want.
expr  `grep -v '^$' FILE.txt  | wc  -l`  -  2
0
 
SayYou_SayMeAuthor Commented:
thanks Wesly_chen

grep -v '^$' FILE.txt | tail -1

can not get the last row, it still get the emty row
0
 
wesly_chenCommented:
grep -v '^[ ]*$' FILE.txt | tail -1    
   There is space in [ ]
0
 
SayYou_SayMeAuthor Commented:
thanks wesly_chen

it seems working

i) how to get the header 8 position charector
ii)how to get the footer 10-13
0
 
wesly_chenCommented:
head -n 1 FILE.txt  |  awk '/???????A/ {print "Good header"}'

grep -v '^[ ]*$' FILE.txt | tail -1  | perl -ne 'print "$1\n" if /.{9}(\d{3})/;'
or
grep -v '^[ ]*$' FILE.txt | tail -1  | perl -ne 'print "footer\n" if /.{9}(\d{3})/;'
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now