0

I have a file abc.txt. I need to grep the file on the date field which is present at 4th position in the text file. I need to split the file based on the date from 1st to 15th August the data need to be in one file and from 16th to 31st August the data need to be in another file.

How can I do that?

a|b|c|08/01/2017|d
a|b|c|08/15/2017|d
a|b|c|08/16/2017|d
a|b|c|08/31/2017|d

Thank you.

4 Answers 4

2

This is easy job with awk:

awk -F'|' '{print >((substr($4,4,2)<=15)?"1-15Aug":"16-31Aug")}' infile

This is checking if the day of month in date was ‎≤15 in filed #4 it will write the lines into the file 1-15Aug otherwise those will redirect to 16-31Aug file using Ternary condition.

The output would be 2 files:

==> 1-15Aug <==
a|b|c|08/01/2017|d
a|b|c|08/15/2017|d

==> 16-31Aug <==
a|b|c|08/16/2017|d
a|b|c|08/31/2017|d
0
1

Using sed:

sed -n -e '\#08/01/2017#,\#08/15/2017#w first.out' \
       -e '\#08/16/2017#,\#08/31/2017#w second.out' file.in

This will write all lines between the line containing 08/01/2017 to the one containing 08/15/2017 (inclusive) to the file first.out. Similarly for the second range of dates, but to second.out.

This relies on the file file.in being sorted in date order.

The # character was chosen as the pattern delimiter as the pattern already contains /.

1

The shortest awk approach:

awk -F'[|/]' '{print >(($5<=15)?"1-15":"16-31")"Aug"}' file
  • -F'[|/]' - complex field separator

Viewing results:

for f in [0-9]*Aug; do echo "file: $f"; cat "$f"; echo; done

The output:

file: 1-15Aug
a|b|c|08/01/2017|d
a|b|c|08/15/2017|d

file: 16-31Aug
a|b|c|08/16/2017|d
a|b|c|08/31/2017|d
1
  • 2
    (assuming the first 3 fields don't contain / characters). Sep 14, 2017 at 16:32
0

You can do a simple csplit if your data is sorted by date.

split -f abc greptest.txt '/08\/16\/2017.*$/'

It will cut your file into two when finding the first Line containing 08/16/2017. They will be named abc00 and abc01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .