![]() |
Distributing data among files
Have:
An input file which contains a 1, 2, or 3 in column 1 of each line. Want: Three output files such that ... output file #1 contains all input lines with a 1 in column 1. output file #2 contains all input lines with a 2 in column 1. output file #3 contains all input lines with a 3 in column 1. This code doesn't work. Code:
# File IdentifiersA secondary issue: Even if this code worked, it reads the input file three times. Can it be made more efficient by making only one pass? awk? Daniel B. Martin |
Sure.
awk can redirect to multiple files. Please check out this URL. http://www.gnu.org/software/gawk/man...ml#Redirection Your code might be. If col1 = 1, then print $0 > file1.txt ; If col1 = 2, then print $0 > file1.txt ; and so on. Ok |
Hi.
Code:
$ cat infile.txt Hope that helps. |
[QUOTE=firstfire;4624386]
Code:
awk '$1 ~ /^[0-9]+$/ {print >"outfile-"$1".txt"}' infile.txtCode:
$ cat '/home/daniel/Desktop/Voters/dbm262inp.txt'Code:
# File Identifications Daniel B. Martin |
Hi.
Here is a one-liner: Code:
awk -F '' '$1 ~ /[0-9]/ {print >"out-"$1}'Inside a shell script: Code:
#!/bin/bash |
Quote:
It was necessary to make minor modifications to suit my program. This is what works. Code:
# File Identifications Daniel B. Martin |
| All times are GMT -5. The time now is 06:14 AM. |