簡體   English   中英

使用 awk 或 sed 從 .csv 中的第四列和第五列中刪除第一個字符

[英]Using awk or sed to remove the first character fom the fourt and fifth column in a .csv

我有一個具有以下值的 .csv 文件:

N0012,17/01/20,13:31:42,N52:01:58,E004:19:00

我需要從第四和第五列中刪除 N 和 E。 所以輸出是:

N0012,17/01/20,13:31:42,52:01:58,004:19:00

我確實嘗試使用

awk '{gsub(/\N|\;/,$4)}1' file

但它也從第一列中刪除了第一個 N。

所以我需要一些幫助。 提前致謝。

你能不能試試以下。

awk 'BEGIN{FS=OFS=","} {sub(/^N/,"",$4);sub(/^E/,"",$5)} 1' Input_file

如果 OP 想要替換任何字母(不僅是 N 或 E),請嘗試以下操作。

awk 'BEGIN{FS=OFS=","} {sub(/^[[:alpha:]]/,"",$4);sub(/^[[:alpha:]]/,"",$5)} 1' Input_file

或者

awk 'BEGIN{FS=OFS=","} {sub(/^./,"",$4);sub(/^./,"",$5)} 1' Input_file

說明:在此處添加對上述代碼的詳細說明。

awk '                ##Starting awk program from here.
BEGIN{               ##Starting BEGIN section of this awk program from here.
  FS=OFS=","         ##Setting field separator and output field separator as comma here.
}
{
  sub(/^N/,"",$4)    ##Using substitute function of awk to substitute starting N in column 4th here.
  sub(/^E/,"",$5)    ##Using substitute function of awk to substitute starting N in column 5th here.
}
1                    ##Mentioning 1 will print edited/non-edited line here.
' Input_file         ##Mentioning Input_file name here.

這可能對你有用(GNU sed):

sed -E 's/^(([^,]*,){3})N/\1/;s/^(([^,]*,){4})E/\1/' file

如果前三個字段后跟N ,則用前三個字段替換匹配項。

如果前四個字段后跟一個E ,則用前四個字段替換匹配項。

鑒於您發布的示例輸入,這就是您所需要的:

$ sed 's/,[NE]/,/g' file
N0012,17/01/20,13:31:42,52:01:58,004:19:00

或者如果您更喜歡 awk:

$ awk '{gsub(/,[NE]/,",")}1' file
N0012,17/01/20,13:31:42,52:01:58,004:19:00

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM