आप सभी रिक्त स्थान, केवल लाइनों एक अल्पविराम है कि में ट्रिम, और awk
उपयोग करना चाहते हैं, तो निम्न आप के लिए काम करेंगे:
awk -F, '/,/{gsub(/ /, "", $0); print} ' input.txt
आप केवल दूसरे कॉलम में रिक्त स्थान को निकालना चाहते हैं , के लिए
awk -F, '/,/{gsub(/ /, "", $2); print$1","$2} ' input.txt
नोट अभिव्यक्ति बदल कि gsub
दूसरी अभिव्यक्ति के साथ //
में चरित्र विकल्प, चर कि तीसरे पैरामीटर है में - और इतने in-place
है - ओ में थर्म शब्द, जब यह हो जाता है, $0
(या $2
) संशोधित किया गया है।
पूर्ण विवरण:
-F, use comma as field separator
(so the thing before the first comma is $1, etc)
/,/ operate only on lines with a comma
(this means empty lines are skipped)
gsub(a,b,c) match the regular expression a, replace it with b,
and do all this with the contents of c
print$1","$2 print the contents of field 1, a comma, then field 2
input.txt use input.txt as the source of lines to process
संपादित मैं कहना चाहता हूँ कि @ बीएमडब्ल्यू समाधान बेहतर है, के रूप में यह वास्तव में ट्रिम केवल प्रमुख और लगातार दो gsub
आदेशों के साथ रिक्त स्थान अनुगामी। क्रेडिट देने के दौरान मैं यह बताता हूं कि यह कैसे काम करता है।
gsub(/^[ \t]+/,"",$2); - starting at the beginning (^) replace all (+ = zero or more, greedy)
consecutive tabs and spaces with an empty string
gsub(/[ \t]+$/,"",$2)} - do the same, but now for all space up to the end of string ($)
1 - ="true". Shorthand for "use default action", which is print $0
- that is, print the entire (modified) line
क्या आप इसे समझा सकते हैं। – Marjer
एक 'gsub' समाधान के लिए @ एडमोर्टन के उत्तर को देखें। यह अंतरिक्ष के लिए चरित्र वर्ग का भी उपयोग करता है जो करने के लिए एक बेहतर चीज है। – codeforester