1

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:

/this/is/path:
file1.txt,date-3
file2.txt,date-2
/this/is/path2:
file.txt,date-1
file2.txt,date-5

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:

/home/name/Documents:
file.txt,12.5.2014-1
file2.txt,15.8.2014-2
/home/name/Music:
file.txt,15.4.2014-2
f2ile3.txt,8.2.2015-5
file2.txt,7.6.2014-3
/home/name/Video:
file.txt,date-5

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.

4
  • It would be much easier to understand if you could give an example..
    – heemayl
    Commented Mar 15, 2015 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? Commented Mar 15, 2015 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
    Commented Mar 15, 2015 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
    Commented Mar 16, 2015 at 13:07

1 Answer 1

0

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
var1=file.txt
var2=15.4.2014
var3=2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .