Reputation: 5
I am using following commands in my script:
max_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | head -1`;
min_length=`awk '{print length}' $File_Path_Name/$filnm | sort -nr | tail -1`;
where the filenm
variable contains the name of the file and File_Path_Name
contains the directory path.
While executing this from script I am getting the error
sort: write failed: standard output: Broken pipe
Any suggestions what I am doing wrong?
Upvotes: 0
Views: 1678
Reputation: 67507
you don't need to scan the file twice for getting max/min try
$ read max min < <(awk '{print length}' file | sort -nr | sed -n '1p;$p' | paste -s)
or you can avoid sorting as well by calculating max/min within awk
$ awk '{len=length}
NR==1 {max=min=len}
max<len{max=len}
min>len{min=len}
END {print max, min}' file
Upvotes: 3