Как я могу найти файл для символа в позиции 12 и, если найден, удалить этот и следующие 5 символов из всех строк? - PullRequest
0 голосов
/ 25 мая 2018

Я пытаюсь проанализировать RSS-канал и сжать информацию в строке, чтобы у меня все еще были дата и время записи, но без миллисекунд или потерянных пробелов, потому что я передаю файл в текстовый обход xscreensaver, которыйограничена читаемой шириной экрана.Я мог бы изменить свой код, чтобы не добавлять 2 строки заголовка до тех пор, пока текст не будет отформатирован, если это будет намного проще.Спасибо за любые идеи ...

The input file at this point looks like this:

ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48:16    Truckers' strike leads to fuel shortages in Brazil
05-24 18:48:16    The marathon atop the world's deepest lake
           ^^^^^^
           Remove these character positions starting from 12 to 17 
           from each title line, with colon in 12 but not from the heading lines

So the result should look like:

ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake

Ответы [ 2 ]

0 голосов
/ 25 мая 2018

После awk может вам помочь.

awk '$2 ~ /[0-9]+:[0-9]+:[0-9]+/{sub(/:[0-9]+ +/,OFS)} 1'  Input_file

Если вы хотите сохранить вывод в самом файле Input_file, добавьте также > temp_file && mv temp_file Input_file в вышеприведенную команду.

Объяснение: Добавление объяснения и здесь.

awk '
$2 ~ /[0-9]+:[0-9]+:[0-9]+/{ ##Checking condition here if 2nd field is matching digit colon digit colon digit pattern then do following.
  sub(/:[0-9]+ +/,OFS)       ##Using substitute function of awk to substitute colon digit(s) then space with OFS whose default value is space in current line.
}
1                            ##awk works on method of condition and then action, so making condition TRUE here and not mentioning action so print will happen.
' Input_file                 ##Mentioning Input_file name here.
0 голосов
/ 25 мая 2018

Мой вариант - заменить двоеточие, за которым следуют две цифры, за которыми следует хотя бы один пробел с одним пробелом:

$ sed 's/:[[:digit:]][[:digit:]]  */ /' file
ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake

.поиск с ^ до начала строки и использование скобок с обратной ссылкой \1.Здесь точка . соответствует произвольному символу:

$ sed 's/^\(..-.. ..:..\):[[:digit:]][[:digit:]]  */\1 /' file
ABC World News Feed
RSS Data retrieved from https:--abcnews.go.com-abcnews-headlines
05-24 18:48 Truckers' strike leads to fuel shortages in Brazil
05-24 18:48 The marathon atop the world's deepest lake
...