LinuxForum.Fans
Тема

Работа с текстом

Автор: Grom 2 сообщений 1616 просмотров
#116798

В одном из текстовых лабораторной работы N1 перенести последнее слово в каждой строке в новую строку. Для строк, состоящих из одного слова - не делать ничего.
N1:
Script started on Thu Sep 5 07:56:10 2002
bash2-2.05$ cat Hum-Dum
Humpty-Dumpty
Set on the wall.
Humpty-Dumpty
Had a greate fall.
And all the king's horses,
And all the king's man.
Can not Humpty,
Can not Dumpty,
Humpty-Dumpty,
Dumpty-Humpty,
Set on this wall
Again.
bash2-2.05$
Script done on Thu Sep 5 07:56:21 2002

Пример:
Script started on Thu Sep 5 08:00:29 2002
bash2-2.05$ pr -n' ' -T Hum-Dum.txt |
> sed -n 's/^ *//p' |
> cut -f1,3 -d' ' |
> sed -n '/[^0-9]/p' >temp01
bash2-2.05$ cut -f1 -d' ' temp01 |
> sed -n 's/$/a\\/p' >temp02
bash2-2.05$ cut -f2 -d' ' temp01 >temp03
bash2-2.05$ paste -d'\n' temp02 temp03 >temp01
bash2-2.05$ sed -f temp01 Hum-Dum.txt >result
bash2-2.05$ rm -f temp*
bash2-2.05$ cat result
Humpty-Dumpty
Set on the wall.
on
Humpty-Dumpty
Had a greate fall.
a
And all the king's horses,
all
And all the king's man.
all
Can not Humpty,
not
Can not Dumpty,
not
Humpty-Dumpty,
Dumpty-Humpty,
Set on this wall
on
Again.
bash2-2.05$
Script done on Thu Sep 5 08:00:47 2002

мне как то надо исправит все это чтобы он переносил тока последнее слово. Хелп плиз)

#117209

[console]$ sed '' t.txt
Humpty-Dumpty
Set on the wall.
Humpty-Dumpty
Had a greate fall.
And all the king's horses,
And all the king's man.
Can not Humpty,
Can not Dumpty,
Humpty-Dumpty,
Dumpty-Humpty,
Set on this wall
Again.
[/console]
[console]~$ sed -r 's/(.+)(\<\w+)/\1\n\2/' t.txt
Humpty-
Dumpty
Set on the
wall.
Humpty-
Dumpty
Had a greate
fall.
And all the king's
horses,
And all the king's
man.
Can not
Humpty,
Can not
Dumpty,
Humpty-
Dumpty,
Dumpty-
Humpty,
Set on this
wall
Again.
[/console]

Войдите или зарегистрируйтесь, чтобы ответить.