Home > Enterprise >  how to use basic linux command to count the number of dot in every line then insert the value in eac
how to use basic linux command to count the number of dot in every line then insert the value in eac

Time:12-28

I process a text file with many domain like abcd.efgh.ijkl.mnop I hope to add a 3 at the same line no matter how it is at the beginning or at the end.

current I use sed 's/[^.]//g' thetextfile | awk '{ print length }' > thenumber then I use librecalc to combine them.

Thank you very much.

CodePudding user response:

This should do it:

while read z;do echo $(echo "$z"|sed 's/[^\.]//g'|wc -m) $z;done < input.txt

which will produce:

3 abcd.efgh.ijkl.mnop
6 abcd.efgh.ijkl.mnop...
4 abcd.efgh.ijkl.mnop.

CodePudding user response:

Using a perl one-liner, taking advantage of tr returning the number of changes:

$ perl -ne 'print tr/././, " ", $_' input.txt
3 abcd.efgh.ijkl.mnop
6 abcd.efgh.ijkl.mnop...
4 abcd.efgh.ijkl.mnop.

CodePudding user response:

In plain bash, without using any external utility:

#!/bin/bash

while read -r line; do
    dots=${line//[^.]}
    printf '%d\t%s\n' ${#dots} "$line" 
done < file
  • Related