tok19
tok19

Reputation: 41

awk create multiple files with included pattern and name from column $3

hello i would like to extend idea based on previous question, that for file which is generated and inluding pattern "START" is generated each file containing name of column $3 with current date

input file1

START
A B 25276 FX M.1 20200421
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
END
START
A B 25060 FX M.1 20200421
END
START
A B 25172 FX M.1 20200421
END
START
A B 25320 FX M.1 20200429
A B 25320 FX M.1 20200421
A B 25320 FX M.1 20200429
A B 25320 FX M.1 20200423
END
START
A B 25173 FX M.1 20200427
A B 25173 FX M.1 20200504
A B 25173 FX M.1 20200429
END

output will be this multiple files eg.

    x_name_25276_20200517
    x_name_25060_20200517
...

..

where generated file for example will contain:

cat x_name_25276_20200517

START
A B 25276 FX M.1 20200421
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200328
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
A B 25276 FX M.1 20200423
END

not found so much of examples how to achieve this partially by only below

awk '/^START/{if (f) close(f); f = "file" ++i} {print > f}'

Upvotes: 0

Views: 38

Answers (1)

karakfa
karakfa

Reputation: 67507

$ awk -v date="$(date +%Y%m%d)" '
       s && NF>2 {s=""; f=FILENAME "_" $3 "_" date; print s0 > f} 
       f         {print > f} 
       /^END/    {close(f); s=f=""}  
       /^START/  {s=1; s0=$0}' file

Upvotes: 1

Related Questions