How to Delete or Grab specific texts inside a file

Joao

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:

  1. It would be OK to delete /Text1/ and delete /Text3 and change : by ;.
  2. Or read what's between 2nd and 3rd / 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.

Kamil Maciorowski

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 14:00 to 16:00. For a moment let's suppose it's true. This allows a straightforward approach:

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Use convert to grab a specific page from a PDF file?

분류에서Dev

How to grab data from table with divs and inputs inside cells?

분류에서Dev

How to delete this file?

분류에서Dev

Check and Delete specific file in a folder and subfolders

분류에서Dev

How to delete an element inside a multidimensional array in PHP?

분류에서Dev

how to delete lines between specific signs in python?

분류에서Dev

How do delete everything before an specific symbol?

분류에서Dev

How would one use wget to grab text between specific tags in a webpage?

분류에서Dev

How to find a specific file and move it to a specific directory?

분류에서Dev

Code for Searching Texts in an external C# file

분류에서Dev

How to enumerate specific rows of a file

분류에서Dev

XML grab attribute from specific element node

분류에서Dev

Grab a specific value from MySQL database

분류에서Dev

How to delete files listed in a text file

분류에서Dev

how to update and delete specified string on the text file

분류에서Dev

How to delete part of a binary file without copying

분류에서Dev

How to delete an invincible 0 byte file?

분류에서Dev

How to delete a corrupted file on an NTFS partition?

분류에서Dev

How to call the Javascript file inside Body tag

분류에서Dev

how to identify the modification in a method inside a java file?

분류에서Dev

How to delete specific rows from a data frame on R.

분류에서Dev

How do I delete specific text in multiple cells at once?

분류에서Dev

MS Access: how to delete everything in a string after two specific characters

분류에서Dev

How can I delete a specific river using NEST?

분류에서Dev

Rails grab template file as string variable in controller

분류에서Dev

How to grep a specific line with quotes and then compare something inside it

분류에서Dev

How To Apply CSS rule to Classes with Specific Keywords Inside Them?

분류에서Dev

How To Apply CSS rule to Classes with Specific Keywords Inside Them?

분류에서Dev

How to set default application for a specific file extension

Related 관련 기사

  1. 1

    Use convert to grab a specific page from a PDF file?

  2. 2

    How to grab data from table with divs and inputs inside cells?

  3. 3

    How to delete this file?

  4. 4

    Check and Delete specific file in a folder and subfolders

  5. 5

    How to delete an element inside a multidimensional array in PHP?

  6. 6

    how to delete lines between specific signs in python?

  7. 7

    How do delete everything before an specific symbol?

  8. 8

    How would one use wget to grab text between specific tags in a webpage?

  9. 9

    How to find a specific file and move it to a specific directory?

  10. 10

    Code for Searching Texts in an external C# file

  11. 11

    How to enumerate specific rows of a file

  12. 12

    XML grab attribute from specific element node

  13. 13

    Grab a specific value from MySQL database

  14. 14

    How to delete files listed in a text file

  15. 15

    how to update and delete specified string on the text file

  16. 16

    How to delete part of a binary file without copying

  17. 17

    How to delete an invincible 0 byte file?

  18. 18

    How to delete a corrupted file on an NTFS partition?

  19. 19

    How to call the Javascript file inside Body tag

  20. 20

    how to identify the modification in a method inside a java file?

  21. 21

    How to delete specific rows from a data frame on R.

  22. 22

    How do I delete specific text in multiple cells at once?

  23. 23

    MS Access: how to delete everything in a string after two specific characters

  24. 24

    How can I delete a specific river using NEST?

  25. 25

    Rails grab template file as string variable in controller

  26. 26

    How to grep a specific line with quotes and then compare something inside it

  27. 27

    How To Apply CSS rule to Classes with Specific Keywords Inside Them?

  28. 28

    How To Apply CSS rule to Classes with Specific Keywords Inside Them?

  29. 29

    How to set default application for a specific file extension

뜨겁다태그

보관