-2

(Need in bash linux)I have a file with numbers like this

1.415949602
91.09582241
91.12042924
91.40270349
91.45625033
91.70150341
91.70174342
91.70660043
91.70966213
91.72597066
91.7287678315
91.7398645966
91.7542977976
91.7678146465
91.77196659
91.77299733
abcdefghij
91.7827827
91.78288651
91.7838959
91.7855
91.79080605
91.80103075
91.8050505

sed 's/^91\.//' file (working) 

Any way possible I can do these 3 steps?

1st I try this

cat input | tr -d 91. > 1.txt (didnt work) 
cat input | tr -d "91." > 1.txt (didnt work) 
cat input | tr -d '91.' > 1.txt (didnt work) 

then

grep -x '.\{10\}' (working)  

then

grep "^[6-9]" (working)

Final 1 line solution

cat input.txt | sed 's/\91.//g' | grep -x '.\{10\}'  | grep "^[6-9]" > output.txt
tripleee
  • 175,061
  • 34
  • 275
  • 318
Priyanka
  • 27
  • 4
  • 3
    What code have _you_ written to solve this and where exactly are you facing issues? – ForceBru Dec 17 '19 at 17:30
  • 3
    On SO we encourage users to add their efforts which they have put in order to solve their own problem, so kindly add so and let us know then. – RavinderSingh13 Dec 17 '19 at 17:30
  • 2
    Just to note, *none* of those numbers have length 10, unless you are replacing a leading `91.` with `0.` rather than just removing `91`. (And in that case, none of them would start with 6, 7, 8 or 9.) – chepner Dec 17 '19 at 17:35
  • @chepner it was a sample of a long file I add few 10 digits number – Priyanka Dec 17 '19 at 17:49
  • 1
    @RavinderSingh13 Please refresh – Priyanka Dec 17 '19 at 17:49
  • @ForceBru please refresh – Priyanka Dec 17 '19 at 17:49
  • 1
    @Priyanka, Good that you have shown your efforts,could you please post samples more clearly also in your question and let us know then? – RavinderSingh13 Dec 17 '19 at 17:54
  • 2
    `tr -d 91.` will remove all `9`s, `1`s, and `.`s from the input, regardless of where they occur. It doesn't remove the specific string `91.`. – chepner Dec 17 '19 at 17:55
  • 1
    All of this should be a single Awk script. Examine `sub()` in the documentation and learn the basics of regular expressions so you don't have to guess so wildly. – tripleee Dec 17 '19 at 18:16
  • `sed` might do, `sed -E 's/^91\.//; /^[0-9]{10}$/!{/^[0-9]+$/d}; /^[0-5][0-9]*$/d'`, see [this demo](https://ideone.com/VbIm6T). – Wiktor Stribiżew Dec 17 '19 at 18:19
  • @WiktorStribiżew when i run it show me sed: 1: "s/^91\.//; /^[0-9]{10}$ ...": extra characters at the end of d command – Priyanka Dec 17 '19 at 18:29
  • @tripleee whats the sub() meaning ? – Priyanka Dec 17 '19 at 18:29
  • @Priyanka So, you do not have a GNU sed then, you need `-e` syntax then. – Wiktor Stribiżew Dec 17 '19 at 18:32
  • @WiktorStribiżew do you want me to replace -E to -e ? (didnt work) . or both (didnt work that too) – Priyanka Dec 17 '19 at 18:35
  • No, changing `E` with `e` makes no sense. You need to study your `sed` implementation if you want to use `sed` solution. – Wiktor Stribiżew Dec 17 '19 at 18:41
  • @WiktorStribiżew I sort out last 2 part, only getting rid off "91." is last, could you please only help with it (please check question again, i updated) – Priyanka Dec 17 '19 at 18:44
  • @chepner I got your point, could you please help me for getting rid off "91." thanks – Priyanka Dec 17 '19 at 18:45
  • Removing `91.` is basic, `sed 's/^91\.//' file` – Wiktor Stribiżew Dec 17 '19 at 18:55
  • Pro tip: include a context word in the search query. I literally googled "awk sub" and that was my top result. – tripleee Dec 17 '19 at 19:55
  • @tripleee no, what I search is "how" .. "how to remove a text in a file in linux " , "how to grep a line in linux based on length" and so on... the problem with pro, they consider everyone else pro or assume that everyone become pro... I am not everyday programmer... I got problem I will solve and back to other work... – Priyanka Dec 17 '19 at 20:28
  • So, what's the expected output? – James Brown Dec 17 '19 at 22:39

1 Answers1

1

Your "final" solution:

cat input.txt |
sed 's/\91.//g' |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt

should avoid the useless cat, and also move the backslash in the sed script to the correct place (and I added a ^ anchor and removed the g flag since you don't expect more than one match on a line anyway);

sed 's/^91\.//' input.txt |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt

You might also be able to get rid of at least one useless grep but at this point, I would switch to Awk:

awk '{ sub(/^91\./, "") } /^[6-9].{9}$/' input.txt >output.txt

The sub() does what your sed replacement did; the final condition says to print lines which match the regex.

The same can conveniently, but less readably, be written in sed:

sed -n 's/^91\.([6-9][0-9]\{9\}\)$/\1/p' input.txt >output.txt

assuming your sed dialect supports BRE regex with repetitions like [0-9]\{9\}.

tripleee
  • 175,061
  • 34
  • 275
  • 318