gpt4 book ai didi

linux - 没有从第一个变量中删除空格

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:24 25 4
gpt4 key购买 nike

此脚本应该从文件中的变量中删除前导和尾随空格,并将它们插入到 SQL 语句中。这适用于除第一个变量之外的每个变量。它不会从用户 ID 中删除尾随空格。我附上了用于收集可变数据的示例文件和代码。

示例文件:

"User ID"|First|Last|Password|"E-Mail Address"|CreateDate|RequestDate|Requestor|Title|"Site Location"|"Client Name"|"Requestor Email"|Roles|Tasks|"Correspondance Packages"
"A145078 "|"James "|Joyce|" DO NOT USE"|jjames@job.com|" 2015/06/22 16:30:00"|"2015/06/20 17:00:00"|"John Edwards"|CSR|Aetna|Aetna|bavolt@aetna.com|"CTM Data Analyst, Medical Appeals Status/Status Reasons, RCA Status and Status Reasons, Grievance Status/Status Reasons, CTM Status/Status Reasons"||

正在生成的输出(见 user_id 后的空格:

INSERT INTO users(user_id, first, last, password, email, createdate, requestdate, requestby)VALUES('A145078 ','James','Joyce','DO NOT USE','jjames@job.com','2015/06/22 16:30:00','2015/06/20 17:00:00','John Edwards');

使用的代码:

    #!/bin/bash
> user_adds.sql

while IFS="|" read -r uid first last password email createdate requestdate requestor title site client requestemail roles tasks packages; do
#printf '%s %s\n' "$uid" "$first" "$roles"
uid=$(echo "$uid" | sed 's/\"//g' | sed 's/^[ \t]*//' | sed 's/[ \t]*$//')
first=$(echo "$first" | sed 's/\"//g' | sed "s/'/ /g" | sed 's/^[ \t]*//;s/[ \t]*$//')
last=$(echo "$last" | sed 's/\"//g' | sed "s/'/ /g" | sed 's/^[ \t]*//;s/[ \t]*$//')
echo $uid - "$uid"
echo $roles

echo "INSERT INTO users(user_id, first, last, password, email, createdate, requestdate, requestby)VALUES('$uid','$first','$last','DO NOT USE','$email','$createdate','$requestdate','$requestor');" >> user_adds.sql

#echo "INSERT INTO user_demographics(user_id, title, site_location, comment, client_name, requester_email,last_password_date_changed)select id,'$title','$site_location','$title','$client','$request_email',NULL from users where user_id in('$user_id');" >> demo_adds.sql


done < 'new_users.csv'

最佳答案

$ cat tst.awk        
BEGIN{ FS="[[:space:]\"]*[|][[:space:]\"]*" }
NR>1 {
gsub(/^[[:space:]"]+|[[:space:]"]+$/,"")
printf "INSERT INTO users(user_id, first, last, password, email, createdate, requestdate, requestby)VALUES('%s','%s','%s','%s','%s','%s','%s','%s');\n", $1,$2,$3,$4,$5,$6,$7,$8
}

$ awk -f tst.awk file
INSERT INTO users(user_id, first, last, password, email, createdate, requestdate, requestby)VALUES('A145078','James','Joyce','DO NOT USE','jjames@job.com','2015/06/22 16:30:00','2015/06/20 17:00:00','John Edwards');

关于linux - 没有从第一个变量中删除空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com