[英]How to remove characters from a csv file using script? step by step execution
我有一個csv(students.csv)文件,其中包含班級學生的姓名。 例如,他們是
sumith123,manu456,siva789...
現在的問題是我想編寫一個腳本從字符名稱中刪除字符,並且只保留整數代替其名稱,即
123,456,789...
我的問題是我可以在哪種平台/技術上實現? 即使我能夠編寫腳本,我也不知道在哪里可以執行此腳本? 是否可以使用Shell腳本來實現這一目標? 如果是,那么任何人都可以從步驟1中進行解釋
您可以使用以下Javascript代碼僅從字符串中提取數字
const regex = /\d+/gm;
const str = `sumith123,manu456,siva789`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
您將需要查看console.log以查看如何檢索該值。 從那里開始,您可以按照自己的意願操作數據。
var name = 'sumith123'; var regex = /\\d+/; var result = name.match(regex)[0]; console.log(result);
如果您在Unix環境中操作,可以使用許多不同的技術來執行此操作。 從命令行, awk
和sed
聯想到彈簧,盡管如果您安裝了Perl
它也會做得很好。
以下應該通過刪除字母來工作:
sed 's/[A-Za-z]*//g' inputFile
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.