How to split a file with delimited string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to split a file with delimited string?
# 1  
Old 03-25-2014
[Solved] How to split a file with delimited string?

I have a unix file text.txt with below content
aaaaa
bbbbbbb
cccccccccc
As of 2013
ddddddddd
eeeeeeeeee
eeeeeeeee
fffffffff
As of 2014
gggggggggggg
hhhhhhhhh
iiiiiiiiiiiiiiii
As of 2016

Now I've to split this file with each file ending with line 'As of' . Please suggest how can I do it?
# 2  
Old 03-25-2014
You could do a while read line, checking if line starts with 'As of'.
If so, write tempvar to file, otherwise fill tempvar.

eg:
Code:
VAR=""
while read line;do
[ "As of" = "${line:0:5}" ] && \
	printf "$VAR" > "$line.txt" && \
	VAR="" || \
	VAR="$VAR\n$line"
done<file.txt

This would produce files named: "As of 2013.txt" "As of 2014.txt" "As of 2016.txt", containing the lines above them from file.txt.

Last edited by sea; 03-25-2014 at 01:01 AM..
This User Gave Thanks to sea For This Post:
# 3  
Old 03-25-2014
Hello Sea,
Thank you for your prompt reply.
Is there anyway it can be done using awk?
Original file is very big.

Thanks.
# 4  
Old 03-25-2014
Quote:
Originally Posted by Steven77
Is there anyway it can be done using awk?
Yes, it can. And this has been answered here before. Try searching the forums.

Regards,
Alister
# 5  
Old 03-25-2014
Hello Alister,
I tried searching, but everywhere I found solution 'not including the delimited text'
but I want to include the 'As of ' line at end of each file.
Is there anyway this can be done?

Thanks.
# 6  
Old 03-25-2014
Try csplit?
Code:
[soliton@UAT:test]$ csplit test.txt "/As of/+1" {*}
36
52
51
0
[soliton@UAT:test]$ cat xx00
aaaaa
bbbbbbb
cccccccccc
As of 2013
[soliton@UAT:test]$ cat xx01
ddddddddd
eeeeeeeeee
eeeeeeeee
fffffffff
As of 2014
[soliton@UAT:test]$ cat xx02
gggggggggggg
hhhhhhhhh
iiiiiiiiiiiiiiii
As of 2016

This User Gave Thanks to soliton For This Post:
# 7  
Old 03-25-2014
Hello Soliton,
Perfect, it worked! Smilie

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk command to split pipe delimited file

Hello, I need to split a pipe de-limited file based on the COLUMN 7 value . If the column value changes I need to split the file Source File Payment|ID|DATE|TIME|CONTROLNUMBER|NUMBER|NAME|INDICATOR 42156974|1137937|10/1/2018|104440|4232|2054391|CARE|1... (9 Replies)
Discussion started by: rosebud123
9 Replies

2. Shell Programming and Scripting

Split a content in a file with specific interval base on the delimited values using UNIX command

Hi All, we have a requirement to split a content in a text file every 5 rows and write in a new file . conditions: if 5th line falls between center of the statement . it should look upto after ";" files are below format: 1 UPDATE TABLE TEST1 SET VALUE ='AFDASDFAS' 2 WHERE... (3 Replies)
Discussion started by: KK230689
3 Replies

3. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

Split a non delimited file into columns depending on user input

I would like some advice on some code. I want to write a small script that will take an input file of this format 111222233334444555666661112222AAAA 2222333445556612323244455445454545 2334556345643534505435345353453453 (and so on) It will be called as : script inputfile X (where X is... (5 Replies)
Discussion started by: onlyforbopi
5 Replies

5. UNIX for Dummies Questions & Answers

how to print the column that contains a string in delimited file in unix

Hi Team, I have a requirement to print the column or find the column number of a particular string in delimited (;) file. Ex: aaa;bbb;ccc;rrr;mmm; gggg;rrr;mmmm;ssss;eee;aaa; ccc;gggg;tttt;bbbb;dddd;ggggg;rrr; my file contains 1000+ rows like above example with semicolon... (3 Replies)
Discussion started by: baskivs
3 Replies

6. Shell Programming and Scripting

Awk search for string pattern in delimited file

I've got a semicolon delimited file. I would like to search for fields that match a pattern, and not hardcoded eg "mth". *th=something If the delimited field fulfills this condition, eg. mth=value I would like to print out both key and value for some number comparison. eg. if value > "12"... (5 Replies)
Discussion started by: alienated
5 Replies

7. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

8. Shell Programming and Scripting

How to split pipe delimited file

I have a pipe delimited input file as below. First byte of the each line indicate the record type. Then i need to split the file based on record_type = null,0,1,2,6 and create 5 files. How do i do this in a ksh script? Pls help |sl||SL|SL|SL|1996/04/03|1988/09/15|C|A|sl||||*|... (4 Replies)
Discussion started by: njgirl
4 Replies

9. Shell Programming and Scripting

To split a string to obtain the words delimited by whitespaces

Please can someone thow some light what is the best way to split a string to obtain the words delimited by whitespaces. (4 Replies)
Discussion started by: Sudhakar333
4 Replies

10. Shell Programming and Scripting

Fast way to split a tab delimited file

I have searched the forum and tried different options. One of the options work but is very slow. The file has millions and millions of records. It is a TAB delimited file which contains two types of records. Metadata and Detail records. M PARTNER 8 LAST_BOOKED_DATE D YYYYMMDD ... (13 Replies)
Discussion started by: madhunk
13 Replies
Login or Register to Ask a Question