简体   繁体   中英

bash script cut/replace guidance

I was trying to do a script that loop reads a file and then perform a process to each line. To cut the rest of the line and save the output in new file, the problem is that the file it reads contain the first word 2 times in the file like

ship2go_db-smart-31-06-2020-15.backup
generator_db-sadm-31-03-2020-20.backup
generator_db-smart-31-03-2020-15.backup
ssdigital_db-sadm-31-03-2020-15.backup
ssdigital_db-smart-31-02-2020-15.backup
tasks_db-sadm-31-03-2020-15.backup
tasks_db-smart-31-04-2020-15.backup
telecom_db-sadm-31-03-2020-15.backup
telecom_db-smart-31-04-2020-15.backup

I want to cut the names of the "file" and save it in new file like

ship2go
generator
ssdigital
tasks
telecom

I am having a lot of ideas and really don't know how to start please give some guidance.

This worked for me.

cat filename |cut -d '_' -f 1|uniq # filename where all your entries are stored

explanation

uniq:- Uniq command is helpful to remove or detect duplicate entries in a file.

cut:- The cut command in UNIX is a command line utility for cutting sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

whenever you are stuck use man command to get details of the command.

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