How to extract patterns form a text files in shell bash

前端 未结 5 1645
无人共我
无人共我 2021-01-21 16:01

I have a text file that contains:

toto.titi.any=val1
toto.tata.any=val2
toto.tete.any=val2

How to extract titi , tata

相关标签:
5条回答
  • 2021-01-21 16:31

    awk and cut are best for this

    you can also read the file line by line, and print out the portion needed.

    $ IFS=.
    $ while read first interested others ; do echo $interested; done < file
    titi
    tata
    tete
    
    0 讨论(0)
  • 2021-01-21 16:36

    Do you really need sed? You could use cut:

    cut -d. -f2 filename
    
    0 讨论(0)
  • 2021-01-21 16:40

    awk/cut would be better choice for this problem.

    here is the sed line and grep option:

    sed -r 's/.*\.([^.]*)\..*/\1/'
    grep -Po '\.\K[^.]*(?=\.)' 
    
    0 讨论(0)
  • 2021-01-21 16:43

    With awk you can do:

    awk -F. '{print $2}' file
    
    0 讨论(0)
  • 2021-01-21 16:46
    sed -n 's/^[^.]*.\([^.]*\)..*/\1/p' myfile.txt
    

    display second value between dot from line having at least 2 dot inside

    0 讨论(0)
提交回复
热议问题