如何使用bash脚本从csv文件中读取特定的整数?(代码片段)

author author     2023-05-13     535

关键词:

我已经写了一个bash脚本(下面)来将数据添加到csv文件,现在我想从这个csv文件中读取一个特定的整数。我该怎么做?

我使用了'cat'命令但它打开了整个文件。但是我不想要那个,我想打开,例如id为1的客户。

另一方面,如何更新csv文件中的文件。例如,客户名称在csv文件中保存为“John”。通过使用更新功能,我想将他的名字改为'Mattew Moniz'。我怎样才能做到这一点?

#!/bin/bash
#Final Project 

while :
do

clear

echo "          ITC 350-Open Source Software Project "
echo ""
echo " ==================================================="
echo "    Customer Account Banking System          "
echo " ==================================================="

echo -e "e[31m1) Create a new customer accounte[0m"       #red
echo -e "e[32m2) Update account datae[0m"                 #green
echo -e "e[33m3) View and manage transactione[0m"         #orange
echo -e "e[34m4) Check customers account detailse[0m"     #blue
echo "5) Delete customer's account"
echo -e "e[36m6) Exite[0m"                                #gray
echo ""
echo ""
echo -e "e[32mPlease choose an option:e[1m"               #ligh green 
read usr_cmd

clear

case $usr_cmd in 

1) echo "Enter customer name: "
read cus_name
while ! [[ $cus_name =~ ^-?[[:alpha:]]+$ || $cus_name =~ " " ]]; do
    echo "Please enter a valid name"
    read cus_name
done


echo "Enter customer DOB: "
read cus_dob


echo "Enter customer national ID number: "
read cus_national_num

while ! [[ $cus_national_num =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer national ID number "
    read cus_national_num
done

echo "Enter customer email address: "
read cus_email

echo "Enter cutomer city: "
read cus_city

while ! [[ $cus_city =~ ^-?[[:alpha:]]+$ || $cus_city =~ " " ]]; do
    echo "Please enter a valid city"
    read cus_city
done

echo "Enter customer country : "
read cus_country

while ! [[ $cus_country =~ ^-?[[:alpha:]]+$ || $cus_country =~ " " ]]; do
    echo "Please enter a valid country "
    read cus_country
done


echo "Enter contact number: "
read cus_phone_num

while ! [[ $cus_phone_num =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer contact number "
    read cus_phone_num
done

echo "Enter type of the account (Saving/Current): "
read cus_account_type


echo "Enter first deposit amount : "
read cus_first_deposit 

while ! [[ $cus_first_deposit =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer first deposit amount "
    read cus_first_deposit
done

;;
# For this part I must write the code to generate a unique customer reg. number automatically
2) echo "Enter the customer registration number to update account data:  "
read cus_reg_num

echo "Enter customer name: "
read cus_name

while ! [[ $cus_name =~ ^-?[[:alpha:]]+$ || $cus_name =~ " " ]]; do
    echo "Please enter a valid name"
    read cus_name
done

echo "Enter customer DOB: "
read cus_dob

echo "Enter customer national ID number: "
read cus_national_num

while ! [[ $cus_national_num =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer national ID number "
    read cus_national_num
done

echo "Enter customer email address: "
read cus_email

echo "Enter customer city: "
read cus_city

while ! [[ $cus_city =~ ^-?[[:alpha:]]+$ || $cus_city =~ " " ]]; do
    echo "Please enter a valid city"
    read cus_city
done


echo "Enter customer country: "
read cus_country

while ! [[ $cus_country =~ ^-?[[:alpha:]]+$ || $cus_country =~ " " ]]; do
    echo "Please enter a valid country "
    read cus_country
done

echo "Enter customer contact number: "
read cus_phone_num

while ! [[ $cus_phone_num =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer contact number "
    read cus_phone_num
done


echo "Enter type of the account (Saving/Current): "
read cus_account_type

while ! [[ $cus_country =~ ^-?[[:alpha:]]+$ || $cus_country =~ " " ]]; do
    echo "Please enter a valid account type "
    read cus_country
done

echo "Enter first deposit amount: "
read cus_first_deposit

while ! [[ $cus_phone_num =~ ^-?[[:digit:]]+$ ]]; do
    echo "Please enter a valid customer first depsit amount: "
    read cus_phone_num
done


;;

#I must write the code to bring changes to the customer's money from csv file
3) echo "Which operation would you like to process with: "

echo "1) Deposit"
read cus_deposit_to_account

echo "2) Withdraw"
read cus_withdraw


;;

#I must write the code to get the data from FINAL_PROJECT.cvs file and display it
4) echo "Enter the customer's registration number to view details: "
read view_cus_reg_num


;;

#I must write the code to delete the customer from FINAL_PROJECT.csv
5) echo "Enter the customer's registration number to delete: "
read cus_reg_num

;;


6)break;;
*) echo "Invalid option";;
esac

echo "Press 6 to quit, anything else to continue: " 
read confirm_exit 
if [ $confirm_exit -eq 6 ]
then break
fi
done



echo "Customer Name", "Customer DOB", "Customer National ID Number", "Customer Email Address", "Customer City", "Customer Country", "Customer Contact Number", "Customer Account Type", "Customer First Deposit Amount" >> FINAL_PROJECT.csv

echo "$cus_name", "$cus_dob", "$cus_national_num", "$cus_email", "$cus_city", "$cus_country", "$cus_phone_num", "$cus_account_type", "$cus_first_deposit" >> FINAL_PROJECT.csv

#awk '!x[$1]++ && ! /^[[:blank:]]*$/' OSSGrades.csv

grep . FINAL_PROJECT.csv | awk '!a[$1]++' >> FINAL_PROJECT.csv
答案

要在第三列中查找ID 1,您可以使用grep

grep '^([^,]*, )21,' file.csv
  • ^匹配线的开头;
  • [^,]匹配除逗号之外的任何字符;
  • *意味着零次或多次,所以[^,]*匹配零个或多个非逗号;
  • 这里使用(...)来分组“列”;
  • 2说前一个元素(上面提到的组)必须重复两次,即id前面有两列。

请注意,这仅适用于简单的CSV文件。对于真实的,可以在单元格中包含引用或转义的逗号甚至换行符,您将需要一种真正的编程语言而不是shell。

如何自动化从具有多个部分的 CSV 文件中读取的脚本?

】如何自动化从具有多个部分的CSV文件中读取的脚本?【英文标题】:HowtoautomatescriptwhichreadsfromCSVfilewithmultiplesections?【发布时间】:2021-08-3003:06:38【问题描述】:我的文本文件中有一个包含整数和非整数行的数据,示例数据如... 查看详情

如何将 CSV 文件从一个 bash 脚本中分离出来

】如何将CSV文件从一个bash脚本中分离出来【英文标题】:howtowriteCSVfilestoseparatefoldersfromonebashscript【发布时间】:2020-12-0922:44:42【问题描述】:我有一个bash脚本,我在其中传递变量,使用“awk”在文件中搜索匹配项,然后将结果... 查看详情

批处理文件:从 .csv 文件中读取浮点值

...特定列(在本例中为第4列)中获取值并找到最高值。该脚本可以很好地处理整数,但是一旦我尝试传入具有浮点数的.csv文件,该脚本只会读取第一个数字。即,1.546=1、0.8 查看详情

如何从 bash 脚本将消息记录到特定路径中的日志文件

】如何从bash脚本将消息记录到特定路径中的日志文件【英文标题】:Howtologmessagestoalogfileinaspecificpathfromabashscript【发布时间】:2012-09-2518:19:45【问题描述】:如何将消息从bash脚本记录到特定路径中的日志文件?一个简单的实现... 查看详情

如何处理 bash 脚本读取的 CSV 文件中的逗号

】如何处理bash脚本读取的CSV文件中的逗号【英文标题】:HowtohandlecommaswithinaCSVfilebeingreadbybashscript【发布时间】:2012-02-1422:56:43【问题描述】:我正在创建一个bash脚本来从CSV文件生成一些输出(我有超过1000个条目,不喜欢手动... 查看详情

如何使用 Bash 从 JAR 中读取 MANIFEST.MF 文件

】如何使用Bash从JAR中读取MANIFEST.MF文件【英文标题】:HowtoreadMANIFEST.MFfilefromJARusingBash【发布时间】:2011-10-2707:42:09【问题描述】:我需要使用bash从“some.jar”中读取MANIFEST.MFmaven清单文件【问题讨论】:jar文件只是zip文件。【参... 查看详情

使用 csv 模块从 csv 文件中读取特定列?

】使用csv模块从csv文件中读取特定列?【英文标题】:Readspecificcolumnsfromacsvfilewithcsvmodule?【发布时间】:2013-05-0609:29:24【问题描述】:我正在尝试解析csv文件并仅从特定列中提取数据。示例csv:ID|Name|Address|City|State|Zip|Phone|OPEID|IP... 查看详情

Bash脚本从.txt文件中删除特定行[重复]

...te]【发布时间】:2018-06-1601:28:21【问题描述】:我想学习如何从txt文件中删除包含用户在变量中键入的单词的行我试过grep-v但随后清除了文件的全部内容帮助?!!!【问题讨论】:sed--in-place\'/somestringhere/d\'your 查看详情

如何使用 Powershell 从 csv 文件中仅读取一列

】如何使用Powershell从csv文件中仅读取一列【英文标题】:HowtoreadonlyonecolumnfromcsvfileusingPowershell【发布时间】:2022-01-0718:08:04【问题描述】:我正在处理powershell脚本,我正在尝试从我的csv文件中仅读取“用户”列,然后将其转换... 查看详情

使用 bash 脚本从 google app 脚本生成的 url 下载 csv 文件

】使用bash脚本从googleapp脚本生成的url下载csv文件【英文标题】:usebashscripttodownloadcsvfilefromaurlgeneratedbygoogleappscript【发布时间】:2014-10-1506:27:45【问题描述】:所有,我的目的是将电子表格文件下载为本地csv文件。经过一些测试... 查看详情

从 CSV 文件中读取特定单元格

...5-0.6250.1718754.5468751.953125-4.890625-3.7031256.359375现在,我正在使用以下代码迭代单元格: 查看详情

如何从字符串变量中获取读取为整数的 CSV 整数

】如何从字符串变量中获取读取为整数的CSV整数【英文标题】:HowtogetCSVintegersreadasintegersfromastringvariable【发布时间】:2020-05-3007:46:36【问题描述】:我有一个字符串,我从xml文件中清理了一些值。$resolutions=strip_tags($resolutions,allo... 查看详情

如何使用 fread 函数读取 CSV 文件的特定行

】如何使用fread函数读取CSV文件的特定行【英文标题】:HowtoreadspecificrowsofCSVfilewithfreadfunction【发布时间】:2014-03-1423:15:51【问题描述】:我有一个双精度的大CSV文件(1000万乘500),我只想读取这个文件的几千行(在1到1000万之... 查看详情

从特定行读取 csv

...一个文件中的另一个数据集是从1985年开始的。在R语言中如何跳过读取1985年之前的数据?【问题讨论】:【参考方案1】:我想你想看看?read.csv以查看所有选项。如果没有看到您的数据样本,很难给出准确的答案。如 查看详情

在 BASH 脚本中使用“awk”将列添加到 CSV 文件的末尾

...k\'inBASHscript【发布时间】:2012-03-1909:25:39【问题描述】:如何使用变量中的字符串在CSV文件的末尾添加一列?输入.csv2012-02-29,01:00:00,Manhattan,NewYork,2342012-02-29,01:00:00,Manhattan,New 查看详情

如何从多个具有相似名称的 .csv 文件中读取数据? [复制]

】如何从多个具有相似名称的.csv文件中读取数据?[复制]【英文标题】:Howtoreaddatafrommultiple.csvfileswithsimilarnames?[duplicate]【发布时间】:2015-12-0405:32:29【问题描述】:我在一个包含许多其他文件的文件夹中有多个以CUSTOMER_YYYYMMDD.c... 查看详情

使用bash shell脚本从文件中查找和提取特定字符串后的值?

】使用bashshell脚本从文件中查找和提取特定字符串后的值?【英文标题】:FindandExtractvalueafterspecificStringfromafileusingbashshellscript?【发布时间】:2020-08-0417:46:18【问题描述】:我有一个包含以下详细信息的文件:文件.txt+--------------... 查看详情

使用 Python 从目录中读取所有 csv 文件

...以下几点:如果我有一个包含ncsv文件的特定文件夹,我如何迭代地读取所有这些文件,一次一个,并对它们的值执行一些计算?例如,对于单个文件,我做这样的事情并对x数组进行一些计算:importcsvimp 查看详情