假设我需要在现有 csv 文件的第一个单元格(不是逐行)中附加以下内容,其中包含客户的详细信息,我如何实现它,
待补充内容: `“这是 Loganayaki,她正在尝试附加 csv 文件
但她做不到,她在使用 shell 脚本时遇到困难
她正在寻求帮助来解决这个问题,以便她完成她的任务。 她尝试了一些对她没有帮助的事情”
客户_文件
ID,Customer_Name,Cust_ADD
1,A,CBE
2,B,POL
3,C,POL`
我尝试了下面的代码
#!/bin/bash
# File paths
csv_file="data.csv"
# New content to prepend
new_content="This is Loganayaki ,she is trying to append the csv file
But she is not able to, she is facing difficulty using shell script
she is seeking help to fix this issue, so that she cab complete her task.
she tried few things which is not helping her"
# Read existing content of the CSV file (excluding the first line)
existing_content=$(tail -n +2 "$csv_file")
# Combine new content with existing content
combined_content="$new_content"$'\n'"$existing_content"
# Write the combined content back to the CSV file
echo "$combined_content" > "$csv_file"
它正在追加,但 new_content 被追加到三个不同的行中 /n 作为空行
我的期望是
`This is Loganayaki ,she is trying to append the csv file
But she is not able to, she is facing difficulty using shell script
she is seeking help to fix this issue, so that she cab complete her task.
she tried few things which is not helping her
ID,Customer_Name,Cust_ADD
1,A,CBE
2,B,POL
3,C,POL`
我认为如果它只是附加到csv文件的第一行,你只需要将
existing_content=$(tail -n +2 "$csv_file")
替换为existing_content=$(cat "$csv_file")
,因为你想保留文件的所有原始内容
#!/bin/bash
# File paths
csv_file="data.csv"
# New content to prepend
new_content="This is Loganayaki ,she is trying to append the csv file
But she is not able to, she is facing difficulty using shell script
she is seeking help to fix this issue, so that she cab complete her task.
she tried few things which is not helping her"
# Read existing content of the CSV file (excluding the first line)
existing_content=$(cat "$csv_file")
# Combine new content with existing content
combined_content="$new_content"$'\n\n\n'"$existing_content"
# Write the combined content back to the CSV file
echo "$combined_content" > "$csv_file"
这会将文件编辑为
This is Loganayaki ,she is trying to append the csv file
But she is not able to, she is facing difficulty using shell script
she is seeking help to fix this issue, so that she cab complete her task.
she tried few things which is not helping her
ID,Customer_Name,Cust_ADD
1,A,CBE
2,B,POL
3,C,POL