【发布时间】:2020-05-10 17:47:30
【问题描述】:
目标:
我需要修改网址以仅保留其中的数字(纬度/经度/id): 在 .csv 文件中,我有一个“标题中的某些标题”。这个我需要找到。在这个找到的标题栏中,我需要删除网址的开头和结尾,所以它只留下一个数字,这是网址的一部分。我需要在不同的结构化 csv 上执行此操作,其中包含具有不同标题和不同 url 模式的几列。有没有办法用 awk 在 bash 中编写函数?
我试过了 - 它没有工作,因为它缺少很多缺失的知识:
#!/bin/bash
CSVFILE=$(find ./aufzubereiten -type f ! -name ".DS_Store") #only one file in this folder.
FILENAME=$(basename "$CSVFILE")
function modify_col() {
COL= how to find the right column in the csv?
awk -F',' OFS="," -v pat='"$PAT"' '{sub(/pat/,X,$${COL})} 1' "$CSVFILE" > "$CSVFILE".tmp1 && mv "$CSVFILE".tmp1 "$CSVFILE"
}
COLTITEL="certain Titel in Header"
PAT='/Text1234Text[0-9]{5,8}Text1.html'
PATNEW=''
modify_col
COLTITEL="certain Titel2 in Header"
PAT='/Text2234Text[0-9]{5,8}Text2.html'
PATNEW=''
modify_col
COLTITEL="certain Titel3 in Header"
PAT='/Text3234Text[0-9]{5,8}Text3.html'
PATNEW=''
modify_col
示例文件:
header1, header2, certain Titel in Header, certain Titel2 in Header, certain Titel3 in Header
,,/Text2234Text7846641Text.html,/Text2234Text8974341Text2.html,/Text2234Text823241Text3.html
,,/Text2234Text7846642Text.html,/Text2234Text8974342Text2.html,/Text2234Text823242Text3.html
,,/Text2234Text7846643Text.html,/Text2234Text8974343Text2.html,/Text2234Text823243Text3.html
结果应该是:
header1, header2, certain Titel in Header, certain Titel2 in Header, certain Titel3 in Header
,,7846641,8974341,823241
,,7846642,8974342,823242
,,7846643,8974343,823243
感谢您的想法:)
【问题讨论】:
-
数据是否真的由文件名中出现 3 次的文字
Text组成?如果“是”,有一些(相对)简单的解决方案(见下文);如果 real 文件名由不同的文本字符串组成,那么我们需要查看一些真实文件名以及有关如何解析所述名称的更多详细信息 -
@markp 你是对的。我不清楚。非常感谢您的努力和想法。在阅读并理解了一些答案之后,我意识到我应该提到在不同的行中是相同的文本,不应该被触摸。这就是为什么我试图“找到”正确的专栏。
-
是的,如果没有可靠的示例(输入和输出),我们会留下很多解释,这就是创建 How to create a minimal, reproducible example 的原因之一 :-)