用另一个名称替换输出的列并将其保存为 .CSV 文件
Replacing a column of an output with another name and saving it as .CSV file
我有一个包含两列的输出,我想用字符串替换其中一列并将其保存为 .csv 文件。
例如文本文件:
year, user, tweet
2009, Katie, I love playing football
2010, James, I play football
2013, Bob, I play basketball
2013, James, I play Baseball
分隔符是',',有3条推文与'play'完全相同,其中2条来自2013年,1条来自2010年。将2013
替换为Development
和2011
与 Early
.
输出应该是:
Early, 1
Development, 2
然后保存到新的 .csv 文件
到目前为止我只能这样做:
$ awk -F, '{IGNORECASE=1} {ARGC=1} /\<play\>/{a[]++} END {for (i in a) print i, a[i]}' Tweet.txt | sort
输出:
2010 1
2013 2
我刚刚开始学习 BASH 非常感谢您的帮助:)
使用 SED
如果我原来的输出是这样的
2010 1
2013 2010
意味着 2013 年,“播放”出现的次数是 2013 次,sed -e 's/2010/Early/' -e 's/2013/Development/'
输出将是:
Early 1
Development Early
你介意进一步帮助我吗?
使用awk
awk -F, '=="2013" && /play/ {="Development"; play++; dev= FS" " play} =="2010" && /play/ {="Early"; play1++; early= FS" " play1} NR > 1 && NF == 2; END { print early"\n" dev > "twitter.csv" }' input_file
$ cat replace.awk
BEGIN {
FS="," #Set the field seperator to comma
} =="2013" && /play/ { #If column1 is 2013 and any column matches play
="Development"; play++; dev= FS" " play #Change column1, count play and create variable
} =="2010" && /play/ { #Same as above
="Early"; play1++; early= FS" " play1
} NR > 1 && NF == 2
END {
print early"\n" dev > "twitter.csv" #Print variables seperated by new line
}
输出
$ awk -f replace.awk input_file
Early, 1
Development, 2
将此 sed
传递给您的尝试
sed -e 's/^2010/Early/' -e 's/^2013/Development/'
整行
awk -F, '{IGNORECASE=1} {ARGC=1} /\<play\>/{a[]++} END {for (i in a) print i, a[i]}' test.txt | sort | sed -e 's/^2010/Early/' -e 's/^2013/Development/'
^
字符告诉 sed 只匹配行首的 201X。
我有一个包含两列的输出,我想用字符串替换其中一列并将其保存为 .csv 文件。
例如文本文件:
year, user, tweet
2009, Katie, I love playing football
2010, James, I play football
2013, Bob, I play basketball
2013, James, I play Baseball
分隔符是',',有3条推文与'play'完全相同,其中2条来自2013年,1条来自2010年。将2013
替换为Development
和2011
与 Early
.
输出应该是:
Early, 1
Development, 2
然后保存到新的 .csv 文件
到目前为止我只能这样做:
$ awk -F, '{IGNORECASE=1} {ARGC=1} /\<play\>/{a[]++} END {for (i in a) print i, a[i]}' Tweet.txt | sort
输出:
2010 1
2013 2
我刚刚开始学习 BASH 非常感谢您的帮助:)
使用 SED
如果我原来的输出是这样的
2010 1
2013 2010
意味着 2013 年,“播放”出现的次数是 2013 次,sed -e 's/2010/Early/' -e 's/2013/Development/'
输出将是:
Early 1
Development Early
你介意进一步帮助我吗?
使用awk
awk -F, '=="2013" && /play/ {="Development"; play++; dev= FS" " play} =="2010" && /play/ {="Early"; play1++; early= FS" " play1} NR > 1 && NF == 2; END { print early"\n" dev > "twitter.csv" }' input_file
$ cat replace.awk
BEGIN {
FS="," #Set the field seperator to comma
} =="2013" && /play/ { #If column1 is 2013 and any column matches play
="Development"; play++; dev= FS" " play #Change column1, count play and create variable
} =="2010" && /play/ { #Same as above
="Early"; play1++; early= FS" " play1
} NR > 1 && NF == 2
END {
print early"\n" dev > "twitter.csv" #Print variables seperated by new line
}
输出
$ awk -f replace.awk input_file
Early, 1
Development, 2
将此 sed
传递给您的尝试
sed -e 's/^2010/Early/' -e 's/^2013/Development/'
整行
awk -F, '{IGNORECASE=1} {ARGC=1} /\<play\>/{a[]++} END {for (i in a) print i, a[i]}' test.txt | sort | sed -e 's/^2010/Early/' -e 's/^2013/Development/'
^
字符告诉 sed 只匹配行首的 201X。