小编kno*_*t22的帖子

每个 LF 之前添加一个 CR

我有一个正在 Windows 计算机上处​​理的文本文件。bcp在使用实用程序将数据从文件加载到数据库表之前,需要删除尾随制表符。

Bash 脚本中的以下命令删除了尾随选项卡:

sed 's/[\t]*$//' < ./input/raw.txt >> ./input/data.txt
Run Code Online (Sandbox Code Playgroud)

CR但它将-转换LFLF导致bcp命令失败的原因。

为了努力保持CR-LF我尝试了这个:

sed 's/[\t]*$/$CR/' < ./input/raw.txt >> ./input/data.txt
Run Code Online (Sandbox Code Playgroud)

但这导致:

在此输入图像描述

期望的结果是:

在此输入图像描述

如何修改命令以获得所需的输出?

bash text-processing newlines

10
推荐指数
2
解决办法
2221
查看次数

如何测试数组中是否存在索引

我正在编写一个 Git Bash 实用程序,它将项目文件夹从一个位置复制到另一个位置。用户可能希望将项目复制到多个目标,但每次执行脚本只允许一个位置。到目前为止的逻辑是这样的 -

#!/bin/bash

# declare and initialize variables
source="/z/files/development/xampp/code/htdocs/Project7"

targets[0]="/z/files/development/xampp/code/htdocs/test/$(date +'%Y_%m_%d')"
targets[1]="/c/users/knot22/desktop/temp_dev/$(date +'%Y_%m_%d')"

# display contents of variables to user
echo "source " $source
echo -e "\nchoice \t target location"

for i in "${!targets[@]}"; do
  echo -e "$i \t ${targets[$i]}" 
done

echo

# prompt user for a target
read -p "Enter target's number for this copy operation: " target
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。接下来我想编写一个if语句来检查用户输入的值是否targettargets. 在 PHP 中它将是array_key_exists($target, $targets). Bash 中的等价物是什么?

bash array

6
推荐指数
1
解决办法
2203
查看次数

标签 统计

bash ×2

array ×1

newlines ×1

text-processing ×1