I've got this problem.

I need to extract some words from text file, which I find by specific pattern in that text. For example:


The pattern would be the /path: and to choose only lines after that and before another /path:. Because there might be the same names of files. And I need to find exact file.txt and take those 3 things:

  • file
  • date
  • number of launch

into 3 variables.

Example: I have text document in format:


and there is directory music and documents which have same files and I want to chose from music only, file.txt. But when it will look for file.txt it will also find one in directory Documents so I only want to choose ones between directory music and it will end in directory Video. And when it finds the file.txt it will save it like this: 1.variable=file.txt, 2.variable=15.4.2014,3.variable=2.

  • It would be much easier to understand if you could give an example.. – heemayl Mar 15 '15 at 23:00
  • What do you mean by “number of launch”? What is “file1.txt date 3” — I understand that you mean that there is a file /this/is/path/file1.txt, but what is that “date 3” bit? – Gilles 'SO- stop being evil' Mar 15 '15 at 23:29
  • that there are 3 information about file and that's name, date when it was edited and how many times it was launched – applenic Mar 15 '15 at 23:31
  • If the file contains empty lines between the sections, please edit and clarify. That makes a huge difference and would greatly simplify things. – terdon Mar 16 '15 at 13:07

Using Awk:

awk -F'[,-]' '
  /Music/ {f=1} 
  /^$/ {f=0} f && /file\.txt/ \
  {printf "%s\n%s\n%s\n", "var1="$1,"var2="$2,"var3="$3}
  ' file

...assuming that there are, in fact, empty lines between your blocks (as per the first example).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.