我寫了以下命令
#!/bin/bash
awk -v value=$newvalue -v row=$rownum -v col=1 'BEGIN{FS=OFS=","} NR==row {$col=value}1' "${file}".csv >> temp.csv && mv temp.csv "${file}".csv
file.csv 的示例輸入
Header,1
Field1,Field2,Field3
1,ABC,4567
2,XYZ,7890
假設$newvalue=3 、$rownum=4 和col=1,那么上面的代碼將替換:
所需輸出
Header,1
Field1,Field2,Field3
1,ABC,4567
3,XYZ,7890
因此,如果我知道行和列,是否可以使用 grep、sed 替換所述值?
編輯 1:欄位 3 將始終為其各自的行提供唯一值。(如果資訊有幫助的話)
uj5u.com熱心網友回復:
假設您的 CSV 檔案與您顯示的內容一樣簡單(帶引號的欄位中沒有逗號),并且您newvalue不包含 sed 將以特殊方式解釋的字符(例如&符號、斜杠或反斜杠),以下內容應該僅適用于 sed (用 GNU sed 測驗):
sed -Ei "$rownum s/[^,]*/$newvalue/$col" file.csv
演示:
$ cat file.csv
Header,1
Field1,Field2,Field3
1,ABC,4567
3,XYZ,7890
$ rownum=3
$ col=2
$ newvalue="NEW"
$ sed -Ei "$rownum s/[^,]*/$newvalue/$col" file.csv
$ cat file.csv
Header,1
Field1,Field2,Field3
1,NEW,4567
3,XYZ,7890
說明:$rownum用作應用以下命令的地址(此處為行號)。s是 sed 替代命令。[^,]*是要搜索和替換的正則運算式:不包含逗號的最長可能字串。$newvalue是替換字串。$col是要替換的事件。
如果newvalue可能包含與號、斜杠或反斜杠,我們必須先對其進行消毒:
sanitizednewvalue=$(sed -E 's/([/\&])/\\\1/g' <<< "$newvalue")
sed -Ei "$rownum s/[^,]*/$sanitizednewvalue/$col" file.csv
演示:
$ newvalue='NEW&\/&NEW'
$ sanitizednewvalue=$(sed -E 's/([/\&])/\\\1/g' <<< "$newvalue")
$ echo "$sanitizednewvalue"
NEW\&\\\/\&NEW
$ sed -Ei "$rownum s/[^,]*/$sanitizednewvalue/$col" file.csv
$ cat file.csv
Header,1
Field1,Field2,Field3
1,NEW&\/&NEW,4567
3,XYZ,7890
uj5u.com熱心網友回復:
隨著sed,怎么樣:
#!/bin/bash
newvalue=3
rownum=4
col=1
sed -i -E "${rownum} s/(([^,] ,){$((col-1))})[^,] /\\1${newvalue}/" file.csv
的結果 file.csv
Header,1
Field1,Field2,Field3
1,ABC,4567
3,XYZ,7890
${rownum}匹配行號。(([^,] ,){n})匹配后跟逗號的非逗號字符組的 n 次重復。然后它應該是目標(要替換)列之前的子字串,通過分配n給col - 1.
uj5u.com熱心網友回復:
讓我們嘗試實作 sed 命令
讓我們考慮一個包含以下內容的示例 CSV 檔案:
$ cat file
Solaris,25,11
Ubuntu,31,2
Fedora,21,3
LinuxMint,45,4
RedHat,12,5
- 洗掉第一個欄位或列:
$ sed 's/[^,]*,//' file
25,11
31,2
21,3
45,4
12,5
此正則運算式搜索一系列非逗號 ([^,]*) 字符并洗掉它們,從而導致第一個欄位被洗掉。
- 要僅列印最后一個欄位,或洗掉最后一個欄位以外的所有欄位:
$ sed 's/.*,//' file
11
2
3
4
5
此正則運算式會洗掉最后一個逗號 (.*,) 之前的所有內容,這會導致洗掉除最后一個欄位之外的所有欄位。
- 僅列印第一個欄位:
$ sed 's/,.*//' file
Solaris
Ubuntu
Fedora
LinuxMint
RedHat
此 regex(,.*) 洗掉從第一個逗號開始到結尾的字符,從而洗掉除最后一個欄位之外的所有欄位。
- 洗掉第二個欄位:
$ sed 's/,[^,]*,/,/' file
Solaris,11
Ubuntu,2
Fedora,3
LinuxMint,4
RedHat,5
正則運算式 (,[^,]*,) 搜索逗號和后跟逗號的字符序列,結果匹配第二列,并用逗號替換此模式,最終洗掉第二列。
注意:洗掉中間的欄位在 sed 中變得更加困難,因為每個欄位都必須逐字匹配。
- 僅列印第二個欄位:
$ sed 's/[^,]*,\([^,]*\).*/\1/' file
25
31
21
45
12
The regex matches the first field, second field and the rest, however groups the 2nd field alone. The whole line is now replaced with the 2nd field(\1), hence only the 2nd field gets displayed.
- Print only lines in which the last column is a single digit number:
$ sed -n '/.*,[0-9]$/p' file
Ubuntu,31,2
Fedora,21,3
LinuxMint,45,4
RedHat,12,5
The regex (,[0-9]$) checks for a single digit in the last field and the p command prints the line which matches this condition.
- To number all lines in the file:
$ sed = file | sed 'N;s/\n/ /'
1 Solaris,25,11
2 Ubuntu,31,2
3 Fedora,21,3
4 LinuxMint,45,4
5 RedHat,12,5
This is simulation of cat -n command. awk does it easily using the special variable NR. The '=' command of sed gives the line number of every line followed by the line itself. The sed output is piped to another sed command to join every 2 lines.
- Replace the last field by 99 if the 1st field is 'Ubuntu':
$ sed 's/\(Ubuntu\)\(,.*,\).*/\1\299/' file
Solaris,25,11
Ubuntu,31,99
Fedora,21,3
LinuxMint,45,4
RedHat,12,5
這個正則運算式匹配“Ubuntu”,直到最后一列除外,并將它們中的每一個都分組。在替換部分,第 1 組和第 2 組以及新編號 99 被替換。
- 如果第一個欄位是“RedHat”,則洗掉第二個欄位:
$ sed 's/\(RedHat,\)[^,]*\(.*\)/\1\2/' file
Solaris,25,11
Ubuntu,31,2
Fedora,21,3
LinuxMint,45,4
RedHat,,5
第一個欄位'RedHat',第二個欄位和剩余的欄位被分組,并且只用第一個和最后一個組進行替換,導致洗掉第二個欄位。
- 在末尾(最后一列)插入一個新列:
$ sed 's/.*/&,A/' file
Solaris,25,11,A
Ubuntu,31,2,A
Fedora,21,3,A
LinuxMint,45,4,A
RedHat,12,5,A
正則運算式 (.*) 匹配整行并將其替換為行本身 (&) 和新欄位。
- 在開頭(第一列)插入新列:
$ sed 's/.*/A,&/' file
A,Solaris,25,11
A,Ubuntu,31,2
A,Fedora,21,3
A,LinuxMint,45,4
A,RedHat,12,5
與上一個示例相同,只是匹配的行后跟新列
我希望這將有所幫助。如果您需要使用 awk 或任何其他命令,請告訴我。謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364827.html
上一篇:在numpy中識別組中的第一個和最后一個非零元素/索引
下一篇:如何在gvim編輯器中獲取行號?
