किसी वर्ण में किसी स्थान पर किसी वर्ण को प्रतिस्थापित करना


0

मेरे पास फाइल इस प्रकार है और कॉमा को दूसरी स्थिति में बदलना होगा जैसे #

1K,1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
1K,2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
1K,3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
1K,8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
1K,9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10,,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11,,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12,,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18,,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03

मैंने यह कोशिश की, लेकिन अल्पविराम से पहले वर्णों को हटाना:

sed 's/^\(.\{2\}\),/\#/' input_file

प्राप्त परिणाम:

#1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
#2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
#3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
#8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
#9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10,,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11,,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12,,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18,,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03

वांछित परिणाम:

1K#1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
1K#2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
1K#3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
1K#8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
1K#9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10,,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11,,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12,,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18,,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03

जवाबों:


1

वास्तव में, जिस अल्पविराम ,को प्रतिस्थापित किया जाना #है, वह स्थिति 3 में है :

sed 's/^\(..\),/\1#/' input_file
  • ^ - स्ट्रिंग एंकर की शुरुआत

  • (..) - 1 पर कब्जा समूह 1 2 वर्णों से युक्त

  • \1 - 1 कैप्चर किए गए समूह का संदर्भ


उत्पादन:

1K#1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
1K#2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
1K#3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
1K#8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
1K#9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10,,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11,,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12,,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18,,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03

1
sed 's/^\(..\),\([0-9]\)/\1#\2/' input_file

लगता है कि आप regexp से परिचित नहीं हैं। यह प्रश्न उतना ही आसान है जितना कि एक प्रश्न के बिना पूछे जाने वाला प्रश्न।


0
use below command to get the desired result

input file u.txt

1K,1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
1K,2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
1K,3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
1K,8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
1K,9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10,,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11,,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12,,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18,,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03


command: sed "s/,/#/1" u.txt

sed "s/,/#/1" u.txt 
1K#1,,,,,,,,,,,0.10000000E+00,0.2837726134152E+08,0.6756896377655E+02
1K#2,,,,,,,,,,,0.10000000E+00,0.2837950666514E+08,0.6757007624345E+02
1K#3,,,,,,,,,,,0.10000000E+00,0.2837975330813E+08,0.6756827852038E+02
1K#8,,,,,,,,,,,0.10000000E+00,0.2610685746631E+08,0.1832495587770E+03
1K#9,,,,,,,,,,,0.10000000E+00,0.2610716318002E+08,0.1836118051089E+03
1K10#,,,,,,,,,,0.10000000E+00,0.2609596327361E+08,0.1822385749588E+03
1K11#,,,,,,,,,,0.10000000E+00,0.2610713453046E+08,0.1841214115744E+03
1K12#,,,,,,,,,,0.10000000E+00,0.2610673990054E+08,0.1846882770826E+03
1K18#,,,,,,,,,,0.10000000E+00,0.2610076439025E+08,0.1854595804085E+03

0

यदि आप एक पैटर्न बना सकते हैं जिसमें हर "के," उपस्थिति "के #" होनी चाहिए, तो आप निम्नलिखित sedप्रतिस्थापन कर सकते हैं :

sed 's/K,/K#/g' input_file

इस विशिष्ट मामले के लिए, मुझे लगता है कि आपका उत्तर काफी सरल है, और दूसरों की तुलना में बेहतर है, जिसमें मेरा भी शामिल है।
ब्रूस
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.