Split a file which a word criteria in two files with awk


 
Thread Tools Search this Thread
Operating Systems Solaris Split a file which a word criteria in two files with awk
# 1  
Old 01-18-2010
Split a file which a word criteria in two files with awk

Hello,

I'm searching with the Awk command to split a file into two others files.
I explain :

in the file N°1 I search the word "NameVirtual" and since that word to the end of the file I want to store all lines in a new file N°2

Also from that word to the beginning of the file I want to store all lines in a file N°3

I have make a command with awk but I find just the line with the word criteria
I'm a beginner with awk

Could you help me for that

Thanks a lot

Christian
# 2  
Old 01-18-2010
One way:

Code:
awk '/NameVirtual/{f=1}{print > f?"file2":"file3"}' file1

# 3  
Old 01-19-2010
Split file in two others files

hello,

i have tried that awk syntax but it doesn't work.
I have also tried other command with grep but no results.

So an example
Word criteria is : NameVirtualHost

file1:

aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
NameVirtualHost
dddddddddddd
eeeeeeeeeee
ffffffffffffffffff

with awk i must put in file2
--------------------------
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc

and in file3
-----------
NameVirtualHost
dddddddddddd
eeeeeeeeeee
ffffffffffffffffff

With grep i can select all line with the Word Criteria but it's not the request

So do you have another idea

thanks a lot

bye
# 4  
Old 01-19-2010
Quote:
Originally Posted by steiner
hello,
I have tried that awk syntax but it doesn't work.
What doesn't work? What errors did you get?

Try nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 01-19-2010
Split a file in two others files

Hello,

thanks for your answers, it's very nice.

The errors are syntax errors but i'm beginner on awk:

i put the command as so:
awk '/NameVirtualHost/ {f=1} {print > f? "toto.txt":"titi.txt"}' httpd.conf

awk: syntax error near line 1
awk: illegal statement near line 1

I have read the awk man but i didn't found whay it is

Thanks, bye
# 6  
Old 01-19-2010
Have you tried it with nawk or /usr/xpg4/bin/awk if you have Solaris?
# 7  
Old 01-19-2010
file ....

HEllo,

i'm trying with nawk because i work on Solaris 10, i will inform you

have a lot of thanks, bye

---------- Post updated at 09:30 AM ---------- Previous update was at 09:26 AM ----------

hello,

here is the result, i not good with nawk

nawk '/NameVirtualHost/ {f=1} {print > f? "toto.txt":"titi.txt"}' httpd.conf
nawk: syntax error at source line 1
context is
/NameVirtualHost/ {f=1} {print > >>> f? <<<
nawk: illegal statement at source line 1

thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

2. Shell Programming and Scripting

Split file into multiple files using awk

I have following file: FHEAD0000000001RTLG20161205110959201612055019 THEAD...... TCUST..... TITEM.... TTEND... TTAIL... THEAD...... TCUST..... TITEM.... TITEM..... TTEND... TTAIL... FTAIL<number of lines in file- 10 digits;prefix 0><number of lines in file-2 - 10 digits- perfix 0>... (6 Replies)
Discussion started by: amitdaf
6 Replies

3. Shell Programming and Scripting

awk to print specific line in file based on criteria

In the file below I am trying to extract a specific instance of path, if the adjacent plugin": "/rundb/api/v1/plugin/49/. Thank you :). file "path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "plugin": "/rundb/api/v1/plugin/49/",... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

Split a big file into multiple files using awk

this thread is a continuation from previous thread https://www.unix.com/shell-programming-and-scripting/223901-split-big-file-into-multiple-files-based-first-four-characters.html ..I am using awk to split file and I have a syntax error while executing the below code I am using AIX 7.2... (4 Replies)
Discussion started by: etldev
4 Replies

5. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

6. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

7. Shell Programming and Scripting

changing the file-name in a directory for all files matching a particular criteria

i have a directory which consist of multiple files out of which there are some files that has -e in their name. I want to write a script that will change all those file-name to -l example there are some files with name : file1-e.wav file2-e.wav file3-english-e.wav file-4-e.wav ... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

8. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

9. Shell Programming and Scripting

split large file based on field criteria

I have a file containing date/time sorted data of the form ... 2009/06/10,20:59:59.950,XAG/USD,Q,1,1115, 14.3025,100,1,1 2009/06/10,20:59:59.950,XAG/USD,Q,1,1116, 14.3026,125,1,1 2009/06/10,20:59:59.950,XAG/USD,R,0,0, , 0,0,0 2009/06/10,20:59:59.950,XAG/USD,R,1,0, 14.1910,100,1,1... (6 Replies)
Discussion started by: asriva
6 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

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)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question