Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert New Line after Pattern 2

Status
Not open for further replies.

ikon1337

IS-IT--Management
Oct 8, 2010
4
GB
I have some data in a txt file, the file is tab delimited, there are 3 columns, Name, Timestamp and Duration the data looks like this.

Refill Liquid 2010/08/21 12:59 00:06:00
Refill Liquid 2010/08/21 11:57 00:06:00
Refill Liquid Im 2010/08/21 11:57 00:06:00
Preassure fault 2010/08/21 11:57 00:06:00
Refill Liquid 2010/08/21 14:57 00:06:00
Refill Liquid Im 2010/08/21 14:57 00:06:00
Pressure Fault 2010/08/21 14:57 00:06:00

'2010/08/21 12:59' is actually 1 column.

i need the data to be broken up into groups based on timestamp, IF timestamp does not match add new blank line so they data will look like this

Refill Liquid 2010/08/21 12:59 00:06:00

Refill Liquid 2010/08/21 11:57 00:06:00
Refill Liquid Im 2010/08/21 11:57 00:06:00
Preassure fault 2010/08/21 11:57 00:06:00

Refill Liquid 2010/08/21 14:57 00:06:00
Refill Liquid Im 2010/08/21 14:57 00:06:00
Pressure Fault 2010/08/21 14:57 00:06:00

is this possible with AWK or SED

thanks

 
Like this ?
Code:
awk -F'\t' 'NR>1 && $2!=s{printf "\n"}{s=$2;print}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the reply, thats not quite what i needed.

any thing with the same timestamp needs to be grouped together i.e contian no line spaces
 
So then perhaps your file isn't TAB-delimited, because PHV's code seems to do the trick for me IF I put your data in a TAB-delimited file...

You may want to try this:

[tt]awk 'NR>1 && $(NF-2)$(NF-1)!=s{printf "\n"}{s=$(NF-2)$(NF-1);print}' /path/to/file[/tt]


HTH,

p5wizard
 
PHV's code does work, i was piping the file through the command, but i was piping through sed first to shift the columns over, i piped though PHV;s code firts and it works like a charm.

Thanks very much, could anyone explain the code to me please?

Thanks
 
[tt]awk [red]-F'\t'[/red] '[blue]NR>1 && $2!=s{printf "\n"}[/blue][green]{s=$2;print}[/green]' /path/to/input[/tt]

[tt][red]-F'\t'[/red][/tt]
use TAB as field delimiter

[tt][blue]NR>1 && $2!=s{printf "\n"}[/blue][/tt]
IF line number is greater than 1 AND field 2 is different from variable s THEN print a newline

[tt][green]{s=$2;print}[/green][/tt]
for all lines:
copy field 2 to variable s
print the line

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top