Strange error while splitting a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange error while splitting a file
# 1  
Old 12-18-2008
Strange error while splitting a file

Hi folks

I am facing a strange error while splitting a '|' delimited file 'file1' based on column '3'. (File is 40 columns wide).

I wish to create as many files 'file_n' from the file 'file1' as distinct values of the column '3'; in 'file1'

eg: file1:

qwe|qweqw|123|fg
fdgg|1ffgfdsg|456|df
qw|we|1|df

output expected:

file_123:
qwe|qweqw|123|fg

file_456:
fdgg|ffgfdsg|456|df

file_1:
qw|we|1|df

Code:

$CUT -d "|" -f3 ${SRC_FILE}|sort|uniq> ${VID_FILE}
for i in `cat ${VID_FILE}`;
do SPLIT_FILE=${file1/1/`echo ${i}`}
echo $SPLIT_FILE
DEST_DIR=${FTP_DIR}/RTM/Outbound


What is happening is for all values of colmun '3' which are '3' charecters wide I am getting correct output but for column value '1' for some reason the output file 'file_1' contains the records with other column values too.

i.e.

file_1:
fdgg|1ffgfdsg|456|df
qw|we|1|df


the bolded record should not have been there
# 2  
Old 12-18-2008
Try...
Code:
awk -F '|' '{print $0 > "outfile_" $3}' infile

# 3  
Old 12-18-2008
Thanks for the response Ygor,

but I am not at all conversant in 'awk' , infact this is my first attempt at shell.

Anyways the problem too is generic i.e. column '3' in input file 'file1' can take say 'n' distinct values, that is why
I have captured the unique fields from column '3' in a 'VID_FILE' .

Now for every value in VID_FILE I am trying to fetch all the records from the 'file1'.For some reason for a particular value '1' (in this example) in 'VID_FILE' which is 1 character less wider than the others is causing the wrong data to get inserted in the file.

I am sure for other records where column '3' might be 4 charcters long I might land up into trouble.
# 4  
Old 12-18-2008
Rephrasing the Q

Let me re-phrase the problem.

I have a '|' delimited flat file with 40 columns say 'input_file'.
I need to split this file 'input_file' into number of files (output file_name = input_file_value_of col_33) based on the column no '33' in 'input_file'.
i.e. for 'n' distinct values of column '33' in 'input_file' we will have 'n' output files each having names 'input_file_n1', 'input_file_n2'....'input_file_n'

where n1, n2, n3 are values of column '33' in 'input_file'.

The script which I wrote works fine only when n1,n2, n3 are all of equal width.

I need help to split the above file for distinct values of column '33'

Last edited by powerslave; 12-18-2008 at 11:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Error while splitting the file

Hi All, I want to split the file after size gets above 100kb. So I am using below command. split -b 100kb File.txt Test But while splitting , my record is breaking as in middle of the record, size of file is getting above 100kb. So after splitting half record is in one file and half... (4 Replies)
Discussion started by: Amey Dixit
4 Replies

2. UNIX for Dummies Questions & Answers

Strange Error

I am receiving an error when i type the command dmesg sudo: sysmon : pam_authenticate: Conversation failure ; TTY=unknown ; PWD=/export/home/sysmon ; USER=root ; COMMAND=/opt/scripts/cronmgmt/synch_crontab.ksh Whats it about and how can I remove it ? (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

3. UNIX for Dummies Questions & Answers

Strange error: grep does not read from file

I have two files file1.txt angie mary susan file2.txt angie blond mary brunnet susan red christine blackI want to get this output angie blond mary brunnet susan redI write grep --file=file1.txt file2.txtand i get no results i also wrote cat file1.txt|while read line... (19 Replies)
Discussion started by: FelipeAd
19 Replies

4. Shell Programming and Scripting

Strange error on mpack from cronjob

I have a shell script which mails a file. It runs fine when executed at prompt, but from cronjob the following line gives an error: $ /opt/local/bin/mpack -s "TTW posts " $FILEPATH/ttw.html myemail@gmail.com Error is : (some junk chars) followed by No such file or directory. Before this... (5 Replies)
Discussion started by: sentinel
5 Replies

5. AIX

Strange error with file access permissions

All, I am trying to copy some data from /admin/reports/Sept/ccn/c_ivsstr01 to /home/users/myhomedir and I am getting an error I have never seen before: The file access permissions do not allow the specified action. The permissions on the file are -rw-r--r-- and I am the owner of the file... (3 Replies)
Discussion started by: kjbaumann
3 Replies

6. UNIX for Dummies Questions & Answers

Strange error

Hi all, i executed a script. cat <filename> | grep <pattern> | awk '{print $1}' | read a i get the answer when i execute it in the shell env (no problem with that) as ./<name>.ksh when i tried the same in cron (as an entry in cron) value is not being read in the variable a through... (1 Reply)
Discussion started by: matrixmadhan
1 Replies

7. Shell Programming and Scripting

ed strange error message

When I start ed as regular user, following message is displayed: $ed ERROR: tempnam failed: Permission denied $ I think, following error produced in vi when search results from previous error: No previous regular expression Setting TMPDIR variable cause no effect. As root all works... (6 Replies)
Discussion started by: frenki
6 Replies

8. Programming

strange error

hi, when i try to compile, i got the following error message, what does it mean? $gcc auto.cpp /usr/tmp/ccmuE12B.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status thanks (1 Reply)
Discussion started by: laila63
1 Replies

9. Shell Programming and Scripting

Strange error.. help

Hi, When I give the following $ ps -ef | grep `echo $var1` | grep -v "grep `echo $var1`" | awk '{print $3}' | grep -v `echo $var1 80892 But when I give the following (made as command substitution) , I get error $var2=`ps -ef | grep `echo $var1` |... (2 Replies)
Discussion started by: preetikate
2 Replies
Login or Register to Ask a Question