jeudi 22 janvier 2015

Strip last character from numeric string conditionally

I have a large file that I need to conditionally remove the last numeric character in only from the lines that start with the numbers 1-19. I want to print out the whole file, line by line, including those lines that do not meet the condition.


Here is my code:



#!/bin/bash

cat > test.txt
118222
1 0.5632 0.10 0.05 1.00 0.45 5.00 0.50 0
0.54 0.49 0.58 0.74 1.08 1.75 1.97 1.86 1.51 1.03 0.76 0.52
0.22 0.20 0.21 0.25 0.37 0.58 0.61 0.64 0.55 0.45 0.39 0.23
0.17 0.20 0.18 0.14 0.10 0.10 0.10 0.10 0.09 0.09 0.11 0.12
5 0.0172 0.10 0.05 1.00 0.45 5.00 0.50 1
0.35 0.32 0.39 0.71 1.15 1.87 1.96 1.72 1.36 0.91 0.70 0.43
0.31 0.30 0.29 0.27 0.36 0.57 0.57 0.57 0.46 0.35 0.34 0.31
0.28 0.29 0.26 0.16 0.11 0.11 0.12 0.11 0.11 0.10 0.14 0.26

FILE=test.text
while read i; do
echo ${i} | grep -E '^([1-9]|1[0-9])' && sed 's/.$//' || echo ${i}
done < $FILE


This doesn't work, although the grep and sed lines work individually.


Any assistance would be greatly appreciated!


Aucun commentaire:

Enregistrer un commentaire