Script to separate file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Script to separate file
# 1  
Old 06-20-2017
Script to separate file

Hi,
Could anyone help me with this please.

Input file --
Code:
ant 1 2 3 4
2 3 4 56 7
dog 8 9 56
ant 2 3 4 5
cvh 6 7 8
ant 1 3 45
78 0 -

Would like to split the file as soon as it encounters the word "ant" very first time.

First Output file--
Code:
ant 1 2 3 4
2 3 4 56 7
dog 8 9 56

Second Output file--
Code:
ant 2 3 4 5
cvh 6 7 8

Third Output file--
Code:
ant 1 3 45
78 0 -

Thanks,
# 2  
Old 06-20-2017
Code:
 csplit filename /ant/ {*}

produces a series of files named xx00 xx01 xx02
the /ant/ part is a regular expression.

See csplit --help for more options. I am assuming linux or GNU coreutils on your machine.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-21-2017
Hello Indira2011, Could you please try following and let me know if this helps you.
Code:
 awk '{print > ($1=="ant"?++count:count)".txt"}'   Input_file OR awk '$1 == "ant"{count++} {print > count".txt"}'   Input_file

Thanks, R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep a log file for words listed in separate text file?

Hello, I want to grep a log ("server.log") for words in a separate file ("white-list.txt") and generate a separate log file containing each line that uses a word from the "white-list.txt" file. Putting that in bullet points: Search through "server.log" for lines that contain any word... (15 Replies)
Discussion started by: nbsparks
15 Replies

2. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

3. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

4. UNIX for Advanced & Expert Users

shell script to send separate mails to different users from a text file

Hi Friends, Could you guys help me out of this problem... I need to send an email to all the users and the email has to be picked from the text file. text file contains the no. of records like: Code: giridhar 224285 847333 giridhar276@gmail.com ramana 84849 33884... (0 Replies)
Discussion started by: giridhar276
0 Replies

5. Shell Programming and Scripting

Calling DB user from separate file in Shell script

Hi All, I need to execute a SQL via shell script and i am connecting to Oracle DB by this way $USERNAME1/$PASSWORD1@$STRING1 and i need to get username, password and string from someother file stored in the Unix Directory. $Username, $Password and $String is stored in File A in Path A and i want... (3 Replies)
Discussion started by: sathish.tn
3 Replies

6. UNIX for Dummies Questions & Answers

need assistance on Calling DB user from separate file in Shell script

Hi All, I need to execute a SQL via shell script and i am connecting to Oracle DB by this way $USERNAME1/$PASSWORD1@$STRING1 and i need to get username, password and string from someother file stored in the Unix Directory. $Username, $Password and $String is stored in File A in Path A and i want... (1 Reply)
Discussion started by: sathish.tn
1 Replies

7. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

8. Shell Programming and Scripting

script to separate bilingual text file

Hi all I have a bilingual text file, one language is English and another one is Assamese. I want to write English language text into one file and Assamese language text into another file. For Example- input file is as dsad জন gdfkghdfkghkdf hksdjhfkjsdfhk hksjdhfksdjf আনজনৰ মনত ghgj jkj... (11 Replies)
Discussion started by: wildhorse
11 Replies

9. Shell Programming and Scripting

Separate String Shell Script

Hi guys, I am a beginner in shell script.. How can I separate a string coming from a parameter in spaces ? Ex: input: test.sh abcd output: a b c d Thanks Dusse (15 Replies)
Discussion started by: dusse
15 Replies

10. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies
Login or Register to Ask a Question