简体   繁体   English

Bash-从TXT生成CSV文件

[英]Bash - Built a CSV File from TXT

I'm newbie in using bash and grep ... I am trying to output a CSV file from a TXT file that contains this lines: 我是使用bash和grep的新手...我正尝试从包含以下行的TXT文件中输出CSV文件:

Input: 输入:

1. Fisrt - Name: Joanna Last - Name: Yang
Place of birth: Paris Date of birth: 01/01/1972 Sex: F
Number: 0009876541234567
2. Fisrt - Name: Bob Last - Name: Lee
Place of birth: London Date of birth: 05/08/1969 Sex: M
Number: 0005671890765223

Output: 输出:

"Joanna","Yang","Paris","01/01/1972","F","0009876541234567"
"Bob","Lee","London","05/08/1969","M","0005671890765223"

Any suggestions would be appreciated!!!! 任何建议,将不胜感激!!!!

Using only one regex with grep won't be easy. 仅使用一个带有grep的正则表达式并不容易。
You can try with multiple regexs and concat the results. 您可以尝试使用多个正则表达式并合并结果。

For instance: 例如:
To get the first names you can use this regex : "Fisrt - Name: ([a-zA-Z]+)" . 要获取名字,您可以使用以下正则表达式: "Fisrt - Name: ([a-zA-Z]+)"
Save this into a variable. 将其保存到变量中。

Next to get the birth dates you can use "birth: ([0-9]+\\/[0-9]+\\/+[0-9]+)" . 接下来要获取出生日期,您可以使用"birth: ([0-9]+\\/[0-9]+\\/+[0-9]+)"
Save this into a variable. 将其保存到变量中。

Do it for each part and concatenate the results with a coma. 对每个部分执行此操作,并用逗号将结果连接起来。

Its clearly not the best way but it's a start. 显然这不是最好的方法,但这是一个开始。 To help with regex you can use https://regex101.com/ . 为了帮助使用正则表达式,您可以使用https://regex101.com/

Maybe try using the sed command line 也许尝试使用sed命令行

If your file is nice and nice formatted, no regex are needed. 如果您的文件格式很好并且格式很好,则不需要正则表达式。
We can read three lines at a time and split them on spaces - we are interested in only specified fields. 我们一次可以读取三行,并在空格处分割它们-我们仅对指定的字段感兴趣。 If you can "assert" that no fields from the file will have spaces (I think no valid human name has spaces in it... right?), you can just do this: 如果您可以“断言”文件中的任何字段都不包含空格(我认为其中没有有效的人名...,对吗?),您可以这样做:

while
    IFS=' ' read -r _ _ _ _ name _ _ _ last &&
    IFS=' ' read -r _ _ _ birthplace _ _ _ birthdate _ sex &&
    IFS=' ' read -r _ number
do
    printf '"%s","%s","%s","%s","%s","%s"\n' \
        "$name" "$last" "$birthplace" "$birthdate" "$sex" "$number"
done <input

Live version available at onlinedbg . 在线版本可在onlinedbg获得

In one line: 一行:

~ $ cat yourfile.txt 
1. Fisrt - Name: Joanna Last - Name: Yang
Place of birth: Paris Date of birth: 01/01/1972 Sex: F
Number: 0009876541234567
2. Fisrt - Name: Bob Last - Name: Lee
Place of birth: London Date of birth: 05/08/1969 Sex: M
Number: 0005671890765223
~ $ sed -r "s/^.*Fisrt - Name: (.*) Last - Name: (.*)$/\1,\2;/g" yourfile.txt | sed -r "s/^Place of birth: (.*) Date of birth: (.*) Sex: (.*)$/\1,\2,\3;/g" | sed -r "s/^Number: (.*)$/\1/g" | sed -n 'H;${x;s/;\n/,/g;s/^,//;p;}' | tail -n +2 > yourfile.csv
~ $ cat yourfile.csv 
Joanna,Yang,Paris,01/01/1972,F,0009876541234567
Bob,Lee,London,05/08/1969,M,0005671890765223
~ $ 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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