LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-11-2012, 09:10 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,165

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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 Identifiers
InFile='/home/daniel/Desktop/Voters/dbm262inp.txt'
OutFile1='/home/daniel/Desktop/Voters/dbm262o1.txt'
OutFile2='/home/daniel/Desktop/Voters/dbm262o2.txt'
OutFile3='/home/daniel/Desktop/Voters/dbm262o3.txt'

for (( i=1; i<=3; i++ ))
do
  OutFile="OutFile"$i
  grep '^$i ' $InFile > $OutFile
done
Please correct the bash if you can.

A 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
 
Old 03-11-2012, 09:35 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 659

Rep: Reputation: 160Reputation: 160
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

Last edited by AnanthaP; 03-11-2012 at 09:36 PM. Reason: typo
 
Old 03-11-2012, 11:04 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

Code:
$ cat infile.txt 
1 text1a
2 text2a
3 text3a
1 text1b
2 text2b
$ awk '$1 ~ /^[0-9]+$/ {print >"outfile-"$1".txt"}' infile.txt
$ cat outfile-1.txt 
1 text1a
1 text1b
$ cat outfile-2.txt 
2 text2a
2 text2b
$ cat outfile-3.txt 
3 text3a
For details about redirection in awk read `info gawk redirection'.

Hope that helps.
 
1 members found this post helpful.
Old 03-12-2012, 11:16 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,165

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
[QUOTE=firstfire;4624386]
Code:
awk '$1 ~ /^[0-9]+$/ {print >"outfile-"$1".txt"}' infile.txt
Thank you, firstfire, for this appealing one-line solution. I've been unable to adapt it to my exiting program. Please point out where I went wrong. This is the input file:
Code:
$ cat '/home/daniel/Desktop/Voters/dbm262inp.txt'
1Alabama
2Alaska
4Arizona
5Arkansas
1California
2Colorado
3Connecticut
4Delaware
3Florida
3Georgia
3Hawaii
3Idaho
4Illinois
4Indiana
This is the code which produces no output (or none that I can find).
Code:
# File Identifications  
InFile='/home/daniel/Desktop/Voters/dbm262inp.txt'
OutFile1='/home/daniel/Desktop/Voters/dbm262o1.txt'
OutFile2='/home/daniel/Desktop/Voters/dbm262o2.txt'
OutFile3='/home/daniel/Desktop/Voters/dbm262o3.txt'
OutFile4='/home/daniel/Desktop/Voters/dbm262o4.txt'
OutFile5='/home/daniel/Desktop/Voters/dbm262o5.txt'

awk '$1 ~ /^[0-9]+$/ {print >$OutFile$1}' $InFile
Thank you.

Daniel B. Martin
 
Old 03-12-2012, 12:43 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

Here is a one-liner:
Code:
awk -F '' '$1 ~ /[0-9]/ {print >"out-"$1}'
I suppose here that the number (digit actually) is a first character on a line and that resulting files share a common prefix. -F option sets field separator to empty string, in which case each character considered a separate field (and $1 is a first character of a line).

Inside a shell script:
Code:
#!/bin/bash
PREFIX='/tmp/out-'
awk -v prefix="$PREFIX" -F '' '$1 ~ /[0-9]/ {print >prefix$1}'
 
1 members found this post helpful.
Old 03-12-2012, 02:41 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,165

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
Quote:
Originally Posted by firstfire View Post
Inside a shell script:
Code:
#!/bin/bash
PREFIX='/tmp/out-'
awk -v prefix="$PREFIX" -F '' '$1 ~ /[0-9]/ {print >prefix$1}'
Nice!

It was necessary to make minor modifications to suit my program. This is what works.
Code:
# File Identifications  
InFile='/home/daniel/Desktop/Voters/dbm262inp.txt'
OutFile1='/home/daniel/Desktop/Voters/dbm262o1.txt'
OutFile2='/home/daniel/Desktop/Voters/dbm262o2.txt'
OutFile3='/home/daniel/Desktop/Voters/dbm262o3.txt'
OutFile4='/home/daniel/Desktop/Voters/dbm262o4.txt'
OutFile5='/home/daniel/Desktop/Voters/dbm262o5.txt'

# Method of LQ member firstfire
PREFIX='/home/daniel/Desktop/Voters/dbm262o'
awk -v prefix="$PREFIX" -F '' '$1 ~ /[0-9]/ {print >prefix$1".txt"}' $InFile
Thank you!

Daniel B. Martin
 
  


Reply

Tags
awk, bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Configure mysql InnoDB to use data files or log files on NFS volumes?? novice32 Linux - Software 4 04-15-2010 09:15 PM
Synchonizing data files and creating a NAS third data set Odyssey1942 Linux - Newbie 5 07-23-2009 07:44 AM
distributing shared object files to client linux emprise Programming 1 02-06-2008 08:52 AM
problem in mounting cdrom(audio files running but not reading data files) amit_usual Linux - Newbie 7 12-29-2007 06:52 AM
Creating/Distributing Linux Binary Files Sava Programming 3 08-29-2006 10:31 AM


All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration