Issue in cat command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in cat command
# 1  
Old 11-05-2010
Issue in cat command

We have shell script (in ksh) which reads the records from a csv file line by line and does some operation. We have below command to read the file (CSV has the absolute path of files stored on a server something like SER/IMP/TEST/2010/12/123465.1).
Code:
P_FILES=`cat $P_BATCH_FILE`
for i in $P_FILES
do
   P_FILEPATH=`echo $i`
   P_FILE=`basename "$i"` #to get file name with extension
   export FILE_NAME=`echo "$P_FILE" | cut -d . -f 1`
   ---then do some operations like insert the entry into database etc
done

The CSV file has around 2000 lines of records. The issue we are seeing is - the script is not reading all the records. It reads about 1190 records and comes out of the loop. We are not sure why it is not reading all lines. We did confirm that the file is correct and there is no special characters in it.

Is there a restriction on CAT command in unix? Does it read only a limited number of lines from a file? Is there a fix for the above issue? What other approach can be used for the above requirement?

Thanks so much for any help you can provide on this.

Thanks,
Shreevatsa

Last edited by Franklin52; 11-05-2010 at 01:43 PM.. Reason: Please use code tags
# 2  
Old 11-05-2010
You have just discovered the size-limit of shell variables on your system. As the output gets large, backticks run out of space. This is one of the many forms of useless uses of cat, too.

Better form, and suitable for files of any size, less likely to strip all spacing out of the input, and more efficient in general is thus:
Code:
while read LINE
do
        do stuff with "$LINE"
done < filename

It reads the file line by line as required instead of sticking it all in memory at once.

It also obeys IFS, so you can split apart records with zero processes created instead of two:

Code:
while IFS="." read FILE EXT
do
        echo $FILE
done < filename


Last edited by Corona688; 11-05-2010 at 01:29 AM..
# 3  
Old 11-05-2010
Thanks

Thank you so much for the reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with cat command on a for loop

Good day to all, I'd like to ask for your advice with regards to this. Scenario : I have here a file named TEST.tmp wherein the value inside is below; "ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE" But when I'm trying to use this in a simple command like; for TESTING in $(cat... (4 Replies)
Discussion started by: asdfghjkl
4 Replies

2. Shell Programming and Scripting

Issue in executing cat (remote ssh)

Hi, I need to ssh remotely to a machine and cat a file assign the value to a variable Script: #!/bin/bash -x value=`cat config.txt` echo "$value" ssh me@xxx.host.com "valu='cat /export/home/test.md5'; echo "$valu"" | tee Execution: $ ./x ++ cat config.txt + value='touch me' +... (5 Replies)
Discussion started by: close2jay
5 Replies

3. Shell Programming and Scripting

Script with ^M causes display issue with cat or more

I have a script to do a couple simple but repetitive commands on files that are provided to us. One of the things is to get rid of the line feeds. This is the section that is causing problems, i even cut this section into its own file to make sure nothing else was affecting it. #!/usr/bin/bash... (4 Replies)
Discussion started by: oly_r
4 Replies

4. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

5. Emergency UNIX and Linux Support

cat issue

I have list of files in my current directory abc.txt 123.csv 234.csv 245.csv 145.csv 123_ex_c.sv I don't want to open first and last file. i.e (abc.txt and 123_ex_csv) I tried cat *.csv, but it won't work. Can anyone tell me the proper regex only in cat Thanks Pritish ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

6. Shell Programming and Scripting

Cat command help

I want to concatenate 100 files to one file and append file name in each record to find out which file it came from for a in $(<shal_group) do cat $a >> bigoutput.group The above code put all files in one file but i want file name appended to each file Record should be like this... (3 Replies)
Discussion started by: pinnacle
3 Replies

7. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

8. UNIX for Advanced & Expert Users

cat command

Dear All I have two text files File1.txt and File2.txt . I am concatenating the two files and making it as single file Cat_File.txt. Now i need to keep joined file in two different path. that is I need to use cat command only once ,but store joined file in two different locations. Since... (3 Replies)
Discussion started by: tkbharani
3 Replies

9. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

10. Shell Programming and Scripting

Issue with Unix cat command

Hi Experts, I am finding the performance of cat command is very wierd, it is taking more time to merge the files into a single file. We have a situation where we would be merging more than 100 files into a single file, but with cat command it is running slow. I tried doing with paste, join... (13 Replies)
Discussion started by: RcR
13 Replies
Login or Register to Ask a Question