bash, extract string before a colon

前端 未结 5 971
暖寄归人
暖寄归人 2020-12-23 01:56

If I have a file with rows like this

/some/random/file.csv:some string
/some/random/file2.csv:some string2

Is there some way to get a file

相关标签:
5条回答
  • 2020-12-23 02:21

    Try this in pure bash:

    FRED="/some/random/file.csv:some string"
    a=${FRED%:*}
    echo $a
    

    Here is some documentation that helps.

    0 讨论(0)
  • 2020-12-23 02:25

    This has been asked so many times so that a user with over 1000 points ask for this is some strange
    But just to show just another way to do it:

    echo "/some/random/file.csv:some string" | awk '{sub(/:.*/,x)}1'
    /some/random/file.csv
    
    0 讨论(0)
  • 2020-12-23 02:27

    Another pure Bash solution:

    while IFS=':' read a b ; do
      echo "$a"
    done < "$infile" > "$outfile"
    
    0 讨论(0)
  • 2020-12-23 02:35

    Another pure BASH way:

    > s='/some/random/file.csv:some string'
    > echo "${s%%:*}"
    /some/random/file.csv
    
    0 讨论(0)
  • 2020-12-23 02:43
    cut -d: -f1
    

    or

    awk -F: '{print $1}'
    

    or

    sed 's/:.*//'
    
    0 讨论(0)
提交回复
热议问题