Remove everything after the first / including the first / for each line

前端 未结 3 1056
轮回少年
轮回少年 2021-01-25 06:52

I have a file with lots of host names. Some have a url part after the host that I\'d like to remove. In other words:

google.com
facebook.com
acme.com/news/frontp         


        
相关标签:
3条回答
  • 2021-01-25 07:55

    Try doing this :

    cut -d '/' -f1 file.txt
    

    or

    awk -F/ '{print $1}' file.txt
    

    or

    perl -F/ -lane 'print $F[0]' file.txt
    
    0 讨论(0)
  • 2021-01-25 07:57

    One way:

    sed 's|/.*||' file
    

    Results:

    google.com
    facebook.com
    acme.com
    bbc.co.uk
    abc.com
    

    You may want to read more about using the slash as a delimiter. HTH.

    0 讨论(0)
  • 2021-01-25 07:57
    awk -F/ '{print $1}' your_file
    

    or

    all the other solutions cannot change the file inplace.but in case of steve you need to add a -i flag for that sed solution.But still it will not work on solaris. below perl solutiopn works on all the platform and replaces the file inplace

    perl -pi -e 's/\/.*//g' your_file
    
    0 讨论(0)
提交回复
热议问题