Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk & sed split file

Tags:

bash

sed

awk

if I have a file test.txt:

example 1 content 2013-3-8:
hello java
example 2 content 2013-4-9:
hello c

how can I use awk or sed to seperate the test.txt to two file

test1

hello java

test2

hello c

I use the command below:

awk '/example/{i++}{print > "test"i}' test.txt

but it will remain the first line(example xxx), can I add some fragment to the print in awk to delete the first line?

like image 850
fudy Avatar asked Dec 26 '22 18:12

fudy


2 Answers

You almost have it:

awk '/^example/ { i++; next } { print >"test"i}'

the next makes awk skip the rest of the statements.

like image 105
Alok Singhal Avatar answered Dec 31 '22 13:12

Alok Singhal


You can use getline to skip the first line. The following should give the desired output:

awk '/example/{getline; i++}{print > "test"i}' test.txt
like image 21
devnull Avatar answered Dec 31 '22 13:12

devnull