简体   繁体   中英

How to remove characters from a csv file using script? step by step execution

I've a csv (students.csv) file contain the name of students from a class. For example they are

sumith123,manu456,siva789...

Now the problem is I want to write a script to remove the characters from their names and I want to keep only the integers in place of their name, that is

123,456,789...

My question is on which platform/technology I can achieve this? Even if I'm able to write the script I have zero knowledge where I can execute this script? Is shell scripting can be used to achieve this? If yes can anyone explain it from the step 1

You can use the following Javascript code to extract only the numbers from the string

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}`);
    });
}

You will need to look at the console.log to see how to retrieve that values. From there on you can manipulate the data as you see fit.

 var name = 'sumith123'; var regex = /\\d+/; var result = name.match(regex)[0]; console.log(result); 

If you are operating in a Unix environment, there are many different technologies you can use to do this. From the command line, awk and sed spring to mind, although if you have Perl installed it will do just as good a job.

The following should work by removing letters:

sed 's/[A-Za-z]*//g' inputFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM