LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 08:10 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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, 08:35 PM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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 08:36 PM. Reason: typo
 
Old 03-11-2012, 10:04 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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, 10:16 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
[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, 11:43 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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, 01:41 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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



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 08:15 PM
Synchonizing data files and creating a NAS third data set Odyssey1942 Linux - Newbie 5 07-23-2009 06:44 AM
distributing shared object files to client linux emprise Programming 1 02-06-2008 07:52 AM
problem in mounting cdrom(audio files running but not reading data files) amit_usual Linux - Newbie 7 12-29-2007 05:52 AM
Creating/Distributing Linux Binary Files Sava Programming 3 08-29-2006 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 PM.

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
Open Source Consulting | Domain Registration