AWK/SED. How to remove parentheses in simple text file

后端 未结 6 2104
无人共我
无人共我 2021-02-07 11:14

I have a text file looking like this:

(-9.1744438E-02,7.6282293E-02) (-9.1744438E-02,7.6282293E-02)  ... and so on.

I would like to modify th

相关标签:
6条回答
  • 2021-02-07 11:26

    I would use tr for this job:

    cat in_file | tr -d '()' > out_file
    

    With the -d switch it just deletes any characters in the given set.

    To add new lines you could pipe it through two trs:

    cat in_file | tr -d '(' | tr ')' '\n' > out_file
    
    0 讨论(0)
  • 2021-02-07 11:30
    cat in_file | sed 's/[()]//g' > out_file
    

    Due to formatting issues, it is not entirely clear from your question whether you also need to insert newlines.

    0 讨论(0)
  • 2021-02-07 11:36

    As was said, almost:

    sed 's/[()]//g' inputfile > outputfile
    

    or in awk:

    awk '{gsub(/[()]/,""); print;}' inputfile > outputfile
    
    0 讨论(0)
  • 2021-02-07 11:37

    Guess we all know this, but just to emphasize:

    Usage of bash commands is better in terms of time taken for execution, than using awk or sed to do the same job. For instance, try not to use sed/awk where grep can suffice.

    In this particular case, I created a file 100000 lines long file, each containing characters "(" as well as ")". Then ran

    $  /usr/bin/time -f%E -o log cat file | tr -d "()"
    

    and again,

    $  /usr/bin/time -f%E -ao log sed 's/[()]//g' file
    

    And the results were:

    05.44 sec : Using tr

    05.57 sec : Using sed

    0 讨论(0)
  • 2021-02-07 11:45

    This might work for you:

    echo "(-9.1744438E-02,7.6282293E-02) (-9.1744438E-02,7.6282293E-02)" |
    sed 's/) (/\n/;s/[()]//g'
    -9.1744438E-02,7.6282293E-02
    -9.1744438E-02,7.6282293E-02
    
    0 讨论(0)
  • 2021-02-07 11:52

    This would work -

    awk -v FS="[()]" '{for (i=2;i<=NF;i+=2) print $i }' inputfile > outputfile
    

    Test:

    [jaypal:~/Temp] cat file
    (-9.1744438E-02,7.6282293E-02) (-9.1744438E-02,7.6282293E-02)
    
    [jaypal:~/Temp] awk -v FS="[()]" '{for (i=2;i<=NF;i+=2) print $i }' file
    -9.1744438E-02,7.6282293E-02
    -9.1744438E-02,7.6282293E-02
    
    0 讨论(0)
提交回复
热议问题