Visit Our UNIX and Linux User Community


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?
# 1  
Old 10-05-2010
How to check if there is only one row in the file?

How can I check if there is only one row in the file? If there are more than one rows, I need to send an email and if there is only one row, do not do anything.


Thanks in anticipation
# 2  
Old 10-05-2010
Code:
if [ "`wc -l <file> | awk '{ print $1 }'`" -ne 1 ]; then
   echo "longer...." | mail -s "subject" user@domain.com
fi

# 3  
Old 10-05-2010
While "wc -l <file" prints "1", you can save a fork and exec with this:

Code:
#!/usr/bin/ksh

exec <$1

read ln
read ln
if [ "$ln" != "" ]
then
 echo "longer...." | mail -s "subject" user@domain.com
fi

# 4  
Old 10-05-2010
Code:
$ ruby -ne 'BEGIN{c=0};c+=1 if $_ !~ /^[[:space:]]+$/; (`echo "long" | mail -s "long" root`;exit) if c==2 ' file

# 5  
Old 10-06-2010
Code:
awk 'END{print (NR>1)?"wrong":X}' infile

# 6  
Old 10-06-2010
Solutions that count all the lines are wasting a lot of time, for big files.

Code:
#!/usr/bin/ksh

sed '
  1d
  s/.*/Longer/
  q
 ' file_in | read zans

if [ "$zans" = "Longer" ]
then
 echo "File grew." | mailx -s "File grew" whoever@wherever.com
fi

# 7  
Old 10-06-2010
Quote:
Originally Posted by DGPickett
Solutions that count all the lines are wasting a lot of time, for big files.
Code:
awk 'NR-1{print "File grew."; exit}' infile | mailx -s "File grew" whoever@wherever.com

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #660
Difficulty: Medium
DOS uses a filesystem which supports 9.3 filenames: 9 characters for the filename and 3 characters for the extension.
True or False?

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

Featured Tech Videos