как мне изменить этот awk-скрипт? Он изменяет каждое вхождение, но должно изменять каждое вхождение, кроме одного столбца. Большая проблема в том, что указанный столбец c не всегда является первым. Но я знаю имя столбца в заголовке.
awk '
BEGIN{
FS=OFS=","
}
FNR==1{
print
next
}
{
for(i=1;i<=NF;i++){
sub(/^\/Text[0-9]+Text/,"",$i)
sub(/Text.*/,"",$i)
}
}
1
' Input_file
Объяснение: Добавление подробного уровня объяснения приведенного выше кода:
awk '
BEGIN{ ##Starting BEGIN section of code here.
FS=OFS="," ##Setting FS and OFS to comma here.
}
FNR==1{ ##Checking condition if FNR==1 then do following.
print ##Printing the current line here.
next ##next will skip all further statements from here.
}
{
for(i=1;i<=NF;i++){ ##Starting a for loop to traverse into all fields here.
sub(/^\/Text[0-9]+Text/,"",$i) ##Substituting from starting Text digits Text with NULL in current field.
sub(/Text.*/,"",$i) ##Substituting everything from Text to till last of field value with NULL in current field.
}
}
1 ##1 will print edited/non-edited line here.
' Input_file ##Mentioning Input_file name here.
Пример файла:
header1, header2, header3-dont-modify-this-column, header4, header5
,,/Text2234Text7846641Text.html,/Text2234Text7846641Text.html,/Text2234Text823241Text.html
,,/Text2234Text7846642Text.html,/Text2234Text7846642Text.html,/Text2234Text823242Text.html
,,/Text2234Text7846643Text.html,/Text2234Text7846643Text.html,/Text2234Text823243Text.html
Результат должен быть:
header1, header2, header3-dont-modify-this-column, header4, header5
,,/Text2234Text7846641Text.html,7846641,823241
,,/Text2234Text7846642Text.html,7846642,823242
,,/Text2234Text7846643Text.html,7846643,823243
Спасибо