Killercode
Killercode

Reputation: 904

Sed File separation with delimiters

I have the following sed commands to split a file in two:

sed -n '1,/(-!-)/{/(-!-)/!p}' file.temp file1.txt
sed -n '/(-!-)/,${/(-!-)/!p}' file.temp file2.txt

the file is like 123 (-!-) 321

and I got this result:

file1: 123

file2: 321

this works just great but I wanted it split to 4 files, it means I will use 3 separators

Can someone give me a hint about how to do it?

Upvotes: 1

Views: 380

Answers (2)

potong
potong

Reputation: 58483

This might work for you:

cat <<! >file.temp
> aaa
> (-!-)
> bbb
> (-!-)
> ccc
> (-!-)
> ddd
> !
csplit -n1 -ffile file.temp '/(-!-)/' '{*}'
sed -i '/(-!-)/d' file?

Upvotes: 1

larsks
larsks

Reputation: 312098

sed is probably not the right tool for this job. awk, on the other hand, is able to split records on arbitrary regular expressions, which ends up making this very easy. First, here's a way to extract a particular section from your file.

Given this input:

this is section 1
(-!-)
this is section 2
(-!-)
this is section 3

We can extract just section three like this:

awk -vRS='\n[(]-!-[)]\n' 'NR==3 {print}' file.temp

Which gives us:

this is section 3

In this command, we're setting awk's record separator (the RS variable) to a regular expression matching the separator in your file. Normally, awk uses a newline as the record separator, so each line is a new record. By setting RS explicitly to this regular expression, we are making awk treat each section as a single entity.

You can extract each section into a separate file like this:

awk -vRS='\n[(]-!-[)]\n' -vprefix="file" '{print > prefix NR}' file.temp

This will give you the files file1, file2, and file3 as output, each containing the content of the appropriate section. The filenames are generated from the value of the "prefix" variable (set by the -vprefix="file" option ) and the current record number.

Upvotes: 5

Related Questions