How to check if there is only one row in the file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to check if there is only one row in the file?
# 8  
Old 10-06-2010
I suppose the ultimate in bulletproof robust is to allow that the second line could be massive, so get just the first line and if the ls -l file size is > (first_line_length + 1), send the message! Since the response is email, we know the speed is not a big concern, but resource use might be. Most tools including sed read ahead, sometimes just the amount of a FILE* default buffer (5120 or 8192 are typical) but sometimes a full line. Luckily, 'line' does not! I assume the file length is correct while the file is open to be actively being extended.

Code:
if (( $( line <file_in|wc -c ) < $( ls -ld file_in |sed 's/  */        /g' |cut -f 5 ) ))
then
 echo ... | mailx ....
fi

Isn't truss so educational? And you can decide you need it after the program starts running and without source code! Here is 'line' in action. How many know dynamic libs are mmap'd so all share those pages but pages are never read if lib entry is not called? Notice that it never hits EOF after the newline:

Code:
$ truss line <<!
> hello
> !
execve("/usr/bin/line", 0x7b117940, 0x7b117948) .......... = 0 [32-bit]
utssys(0x7b119aa0, 0, 0) ................................. = 0
open("/usr/lib/dld.sl", O_RDONLY, 074444) ................ = 4
read(4, "02\v010e0512@ \0E 1d0214\0\0\0\0".., 128) ....... = 128
lseek(4, 128, SEEK_SET) .................................. = 128
read(4, "10\0\004\0\0\0( \0033 94\0\0\0\0".., 48) ........ = 48
mmap(NULL, 209812, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_SHLIB|MAP_STATICPREDICTION, 4, 40960) = 0xc0010000
mmap(NULL, 17752, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_SHLIB, 4, 253952) = 0x7b0eb000
close(4) ................................................. = 0
sysconf(_SC_CPU_VERSION) ................................. = 532
utssys(0x7b119ea0, 0, 0) ................................. = 0
getuid() ................................................. = 3080 (3080)
getuid() ................................................. = 3080 (3080)
getgid() ................................................. = 6900 (6900)
getgid() ................................................. = 6900 (6900)
mmap(NULL, 8192, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7b0e9000
open("/opt/graphics/OpenGL/lib/libogltls.sl", O_RDONLY, 0200) ERR#2 ENOENT
open("/usr/lib/libc.2", O_RDONLY, 0200) .................. = 4
fstat(4, 0x7b11a748) ..................................... = 0
read(4, "0210010e0512@ \0\0\0\0\0\0\0\0\0".., 128) ....... = 128
lseek(4, 128, SEEK_SET) .................................. = 128
read(4, "10\0\004\0\0\0( \012e3< \0\010\0".., 48) ........ = 48
read(4, "80\0\0\v\0\0\004\0\0\0\0", 12) .................. = 12
mmap(NULL, 1241088, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_SHLIB, 4, 290816) = 0xc0100000
mmap(NULL, 57344, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_SHLIB, -1, 0) = 0x7b0db000
mmap(0x7b0d2000, 36864, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_SHLIB, 4, 1531904) = 0x7b0d2000
mmap(NULL, 16384, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7b0ce000
close(4) ................................................. = 0
open("/usr/lib/libdld.2", O_RDONLY, 0200) ................ = 4
fstat(4, 0x7b11a948) ..................................... = 0
read(4, "02\v010e0512@ \0E 1d0217\0\0\0\0".., 128) ....... = 128
lseek(4, 128, SEEK_SET) .................................. = 128
read(4, "10\0\004\0\0\0( \0\0) \ \0\010\0".., 48) ........ = 48
read(4, "80\0\0\v\0\0\004\0\0\0\0", 12) .................. = 12
mmap(NULL, 12288, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_SHLIB, 4, 8192) = 0xc0003000
mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_SHLIB, 4, 20480) = 0x7b0cd000
close(4) ................................................. = 0
mmap(NULL, 16384, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7b0c9000
mmap(NULL, 88, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7b0c8000
sigsetreturn(0x7b0cfd56, 0x6211988, 1392) ................ = 0
read(0, "h ", 1) ......................................... = 1
read(0, "e ", 1) ......................................... = 1
read(0, "l ", 1) ......................................... = 1
read(0, "l ", 1) ......................................... = 1
read(0, "o ", 1) ......................................... = 1
read(0, "\n", 1) ......................................... = 1
alarm(0) ................................................. = 0
hellowrite(1, "h e l l o ", 5) ................................ = 5

write(1, "\n", 1) ........................................ = 1
exit(0) .................................................. WIFEXITED(0)
$


Last edited by DGPickett; 10-06-2010 at 02:14 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

2. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

3. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

4. Shell Programming and Scripting

Request to check:remove entries with duplicate numbers in first row

Hi I have a file 1 xyz 456 1 xyz 456 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 4 ghf 658 I want the output 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 (3 Replies)
Discussion started by: manigrover
3 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Adjacent row and column check in Perl

HI, i am new to perl world. And i am trying to compress a file, as given below procedure. INPUT FILE: 1 1 2 1 ==> R1 2 1 3 1 ==> R2 3 1 4 1 ==> R3 OUTPUT FILE: 1 1 4 1 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

7. Shell Programming and Scripting

awk to check begining of each row

Hi, I want to insert columns into a file. Some are standard (e.g. 1) and some are dependent on the contents on other columns. I am using the following code In the below the first 1 (after $2), can be a 1 or 3 depending on what $1 starts with e.g. if $1 is 405 then the third field in the... (7 Replies)
Discussion started by: ksexton
7 Replies

8. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

9. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question