将数据从一个文件追加到另一个文件

将数据从一个文件追加到另一个文件

所以我有一个脚本来创建一个如下所示的 CSV 文件:

Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity
1800 UTC MAY 16,
0000 UTC MAY 17,
0600 UTC MAY 17,
1200 UTC MAY 17,
1800 UTC MAY 17,
0000 UTC MAY 18,
0600 UTC MAY 18,
1200 UTC MAY 18,
1800 UTC MAY 18,
0000 UTC MAY 19,
0600 UTC MAY 19,
1200 UTC MAY 19,
1800 UTC MAY 19,
0000 UTC MAY 20,
0600 UTC MAY 20,
1200 UTC MAY 20,
1800 UTC MAY 20,
0000 UTC MAY 21,

我想要做的是编辑我的代码,以便我可以从另一个文件添加数据,如下图第二张图所示,并将其附加到每行的末尾,以便它匹配。

28.0
28.9
29.6
30.3
31.0
31.9
33.1
34.4
35.5
36.2
36.8
37.0
36.9
36.2
35.5
34.6
33.7
32.8

我当前的代码在这里:

#! /bin/bash
inputFileString=$1
outputFileString=$2
touch "$2"
chmod 755 "$2"
echo "" > lat.txt
sed -i '1d' lat.txt
rm $2
test=(0,1,2,3,4,5,6,7,8,9)
echo "Converting $inputFileString -> $outputFileString ..."
echo "" >  $2
awk -F '[<>]' '/<lat>/ {print $3}' $1 >> lat.txt
grep -Eo '<dtg>.*</dtg>' $1 | cut -c 6-20  >> $2
sed -i 's/$/,/' $2
#awk '{print $0, "$variable"}' $2 >> $2
#awk -F '[<>]' '/<lon>/ {print $3}' $1 >> $2
sed -i '1d' $2
sed -i '1i Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity' $2
echo Done!

仅供参考,我的代码接受 kml 文件的输入并提取特定数据,并使用该数据生成 csv 文件。

答案1

使用 Raku(以前称为 Perl_6)

raku -e 'my @a = @*ARGS[0].IO.lines.skip; my @b = @*ARGS[1].IO.lines;
         "Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity".put; 
         .put for [Z] @a, @b;' ethan1.txt ethan2.txt

示例输出:

Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity
1800 UTC MAY 16, 28.0
0000 UTC MAY 17, 28.9
0600 UTC MAY 17, 29.6
1200 UTC MAY 17, 30.3
1800 UTC MAY 17, 31.0
0000 UTC MAY 18, 31.9
0600 UTC MAY 18, 33.1
1200 UTC MAY 18, 34.4
1800 UTC MAY 18, 35.5
0000 UTC MAY 19, 36.2
0600 UTC MAY 19, 36.8
1200 UTC MAY 19, 37.0
1800 UTC MAY 19, 36.9
0000 UTC MAY 20, 36.2
0600 UTC MAY 20, 35.5
1200 UTC MAY 20, 34.6
1800 UTC MAY 20, 33.7
0000 UTC MAY 21, 32.8

上面是用 Raku(Perl 编程语言家族的成员)编写的答案。时间戳文件从命令行读取@*ARGS[0](时间戳标题行是skip-ped 并稍后添加回来)。数字列从命令行读取为@*ARGS[1]。每个文件都转换为一个IO对象,分解lines并存储为数组@a@b.然后@a@b[Z]按元素压缩在一起 和put

https://docs.raku.org/routine/Z
https://raku.org

答案2

不知道您在期待什么,但我认为paste这就是您正在寻找的:

sed '1s!^!\n!' another.txt | paste -d '\0' input.csv -
Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity
1800 UTC MAY 16,28.0
0000 UTC MAY 17,28.9
0600 UTC MAY 17,29.6
1200 UTC MAY 17,30.3
1800 UTC MAY 17,31.0
0000 UTC MAY 18,31.9
0600 UTC MAY 18,33.1
1200 UTC MAY 18,34.4
1800 UTC MAY 18,35.5
0000 UTC MAY 19,36.2
0600 UTC MAY 19,36.8
1200 UTC MAY 19,37.0
1800 UTC MAY 19,36.9
0000 UTC MAY 20,36.2
0600 UTC MAY 20,35.5
1200 UTC MAY 20,34.6
1800 UTC MAY 20,33.7
0000 UTC MAY 21,32.8

input.csvanother.txt从您的问题描述中复制。

答案3

在任何类似 bourne 的 shell 中使用任何版本的 echo、cat 和 Paste:

$ { echo ""; cat file2; } | paste -d '' file1 -
Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity
1800 UTC MAY 16,28.0
0000 UTC MAY 17,28.9
0600 UTC MAY 17,29.6
1200 UTC MAY 17,30.3
1800 UTC MAY 17,31.0
0000 UTC MAY 18,31.9
0600 UTC MAY 18,33.1
1200 UTC MAY 18,34.4
1800 UTC MAY 18,35.5
0000 UTC MAY 19,36.2
0600 UTC MAY 19,36.8
1200 UTC MAY 19,37.0
1800 UTC MAY 19,36.9
0000 UTC MAY 20,36.2
0600 UTC MAY 20,35.5
1200 UTC MAY 20,34.6
1800 UTC MAY 20,33.7
0000 UTC MAY 21,32.8

或者在每个 Unix 机器上的任何 shell 中使用任何 awk:

$ awk 'NR==1{print} NR==FNR{a[NR-1]=$0; next} {print a[FNR] $0}' file1 file2
Timestamp,Latitude,Longitude,MinSeaLevelPressure,MaxIntensity
1800 UTC MAY 16,28.0
0000 UTC MAY 17,28.9
0600 UTC MAY 17,29.6
1200 UTC MAY 17,30.3
1800 UTC MAY 17,31.0
0000 UTC MAY 18,31.9
0600 UTC MAY 18,33.1
1200 UTC MAY 18,34.4
1800 UTC MAY 18,35.5
0000 UTC MAY 19,36.2
0600 UTC MAY 19,36.8
1200 UTC MAY 19,37.0
1800 UTC MAY 19,36.9
0000 UTC MAY 20,36.2
0600 UTC MAY 20,35.5
1200 UTC MAY 20,34.6
1800 UTC MAY 20,33.7
0000 UTC MAY 21,32.8

相关内容