Linux Centos 6-64
File1.txt
has several lines in this format:
/Text1/Text2/Text3:Text4
How to do to have file Result.txt
to be with all lines as follows?
Text2;Text4
PS: the :
transforms into ;
.
I thought of two ways of doing this:
/Text1/
and delete /Text3
and change :
by ;
./
as Text2
then read what's after :
which is Text4
and do ;Text4
.So as final result the file Result.txt
would have
Text2;Text4
Either way would work, better the one that works faster.
PS: The texts may have spaces, symbols and points inside, but the original separators from the texts are always /
/
/
:
which don't appear anywhere else inside the texts.
This to be done in all lines of File.txt
.
Files:
File1.txt
/Soccer.Teacher/Michael.John/Group1:monday-friday - 14h to 16h
/Basketball.Teacher/Susana.Stevens/Group2:tuesday-thursday-3pm-to-5pm
/Tennis.Teacher/Josh.Karen/Group3:monday-wednesday-5pm_to_7pm
etc.
Result.txt
to be:
Michael.John:monday-friday - 14:00 to 16:00
Susana.Stevens:tuesday-thursday-3pm-to-5pm
Josh.Karen:monday-wednesday-5pm_to_7pm
etc.
The texts may have spaces, symbols and points inside, but the original separators from the texts are always
/
/
/
:
which don't appear anywhere else inside the texts.
Not true. You gave an example with This allows a straightforward approach:14:00 to 16:00
. For a moment let's suppose it's true.
tr ':' '/' | cut -d '/' --output-delimiter=';' -f 3,5
tr
unifies delimiters, then cut
picks the right fields.
Notes:
--output-delimiter
is not required by POSIX. If your cut
doesn't support it, this is the alternative:
tr ':' '/' | cut -d '/' -f 3,5 | tr '/' ';'
Note we cannot use tr ':' ';'
as the first command in the pipe (which would simplify the rest) because you don't guarantee that ;
doesn't appear in the input (cut
will get confused if it does).
cut
takes fields 3 and 5 (not 2 and 4) because everything before the first separator is already field 1 (empty string in your case).
However, if only the first :
should be a separator then the command may be:
sed 's|:|/|' | cut -d '/' --output-delimiter=';' -f 3,5
sed 's|:|/|'
will replace only the first occurrence of :
(opposed to sed 's|:|/|g'
which is equivalent to our original tr ':' '/'
).
In case you are not familiar with a standard way to use such filters with files, this is the right syntax (with sed
and POSIX-compliant cut
):
< File1.txt sed 's|:|/|' | cut -d '/' -f 3,5 | tr '/' ';' > Result.txt
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다