I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file is a configuration file for several elements and hence consists of a repeated configuration pattern like this:
#fruit apple
#color green
#surface smooth
size 7cm
#fruit grape
#color green
#surface smooth
size 2cm
I want to split the file in 2 as equal as possible pieces but a split has to be done at the start of an element (starting with a #fruit entry). If the configuration file has an odd number of entries it should allow one more item in one of the files, and if not should split so that the 2 resulting files will have the same amount of items.
The tags like "#fruit" are unique so they can be used in e.g. "grep" combined with "wc -l" to find amount of items and at which element to split.
It's not different, but since I received no answer on that query I decided to write the problem in a different way since maybe it was difficult to understand what I meant. I have a feeling though that this should be an easy task to solve, but I am stuck. I have determined at which element I should split by grep'ing for "#fruit" to find number of elements and using "expr" and "/" to get the closest integer value of the number of the element where I should split. But from there I am unsure about the rest. I have a feeling that awk should be the way to go but I am not sure how. Another option is to find the line number of the start of the element where I should cut.
grep for #fruit then get a count with wc -l. Your source is structured with 5 lines for each entry so divide the number of #fruit entries found by 2 then multiply that by 5 using bc. You can then use the split -l command to make your two files using those results. I would add something to make sure none of the lines go missing.
grep for #fruit then get a count with wc -l. Your source is structured with 5 lines for each entry so divide the number of #fruit entries found by 2 then multiply that by 5 using bc. You can then use the split -l command to make your two files using those results. I would add something to make sure none of the lines go missing.
Thanks!
I think this is close to a way to do it. How can x5 help me to find correct place to cut?
Something like this might work:
- Filter out heading or trailing newlines to assure the count will be correct.
- Grep for "#fruit" and pipe it through "wc -l" to get amount of "#fruit" - elements.
- If number is even number I can split in middle
"wc -l"/2.
- If number is odd I can split at:
("wc -l"/2)+3 lines
And then I probably have to adjust 1 lines up or down to get the split exact.
Hmm...if this works I need to find out if a number is odd or even.
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
An awk script may be useful. There is a special variable "RS", Record Separator, that may be be set to read "paragraphs", i.e. groups of lines separated by an empty line:
That would allow you to treat your file as essentially just a number of such records.
With your calculated knowledge of where you want to the split to be, the "pattern" part of an awk statement:
should allow you to complete the solution with the use of another builtin variable "NR", Number of Record. This is because the pattern part may be a logical expression, such as:
the action might be something as simple as print ... cheers, drl
A) I would like to achive following actions using shell script. can someone help me with writing the shell script
1) Go to some dir ( say /xyz/logs ) and then perform find operation in this dir and list of subdir using
find . -name "*" -print | xargs grep -li 1367A49001CP0162 >... (1 Reply)
Hi
This is my third past and very impressed with previous post replies
Hoping the same for below query
How to find a existing file location and directory location in solaris box (1 Reply)
I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt.
Please help me how to do this in shell script. (1 Reply)
Hi,
I am logging to a linux server through a user "user1" in /home directory.
There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory.
When the script is added to crontab, on... (1 Reply)
Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Hi all
I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far:
File A
line 1
line 2
line 3
line 4
... (6 Replies)
I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure,
say location 1,
/home/rick/tmp_files/1-12/00-25/
here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this.
For example:
split -l 3000000 filename.txt
This is very slow and it splits the file with 3 million records in each... (10 Replies)
I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file consists of groups of lines separated by newline. The file can vary in length, so I need to check number of groups of text. Here's an example
====EXAMPLE START====
#fruit banana
#color yellow
#surface smooth... (0 Replies)