Div
Div

Reputation: 11

Split file after a string

I have a file in aix which I want to split after the following string:

"Snapshot timestamp = 11/25/2014 16:00:00"

I tried using command below however I get error due to the space in the string.

awk '/YOUR_TEXT_HERE/{n++}{print >"out" n ".txt" }' final.txt

It would also helpful if I can get a syntax to split data 'before' the string as well.

Upvotes: 1

Views: 236

Answers (1)

JJoao
JJoao

Reputation: 5347

The problem is not spaces it is the "/".

Have you protected the "/" in the date?

(/...11\/25\/2014.../)

(with that it is working for me)

awk '/Snapshot timestamp = 11\/25\/2014 16:00:00/ {n++}{print >"out" n ".txt" }' final.txt

As @Etan Reisner point out,

awk '$0 == "Snap..." {n++; next } {print >"out" n ".txt" }' final.txt

is a better solutions (you don't have to protect the regex operators). The "next" instruction will "remove" the timestamp from the output files.

If you plan to do that again in the future I suggest a awk scritp:

#!/usr/bin/gawk -f

BEGIN                      { n =1                  }
/Snapshot timestamp = /    { n++; next             }
                           { print >"out" n ".txt" }

Usage

awk -f awkscript  final.txt

or

awkscript  final.txt    (after chmod...)

Upvotes: 2

Related Questions