Hi
I know it was more than a year, but I'll just leave the answer here maybe it will help someone.
With the modified code of yours, the script will only print the alignments that have hardclip or softclip, that means you'll loose the header, which is what samtools complains about after you pipe tha awk result to it.
The solution is to either get the header first, concatenate it with the awk output and export as BAM:
samtools view -H in.bam > header.txt
samtools view in.bam | awk '$6 ~ /H|S/{print} > clipped.txt
cat header.txt clipped.txt | samtools view -bS > clipped.bam
Or if you want to solve it with one command, you can just modify the command to include header as well:
samtools view -h in.sam | awk '$6 ~ /H|S/{print}; $1 ~ /@/{print}' | samtools view -bS - > clipped.bam