scanning the file for a particular column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers scanning the file for a particular column
# 1  
Old 01-10-2011
scanning the file for a particular column

I have a file containing 4 columns.
need to scan that file, if all the rows in the column4 have a value ZERO, it should print "everything is fine".
And if all are not ZERO , at the first encounter of non ZERO value of 4th column it should print "some problem "

may be a silly question, but at current moment.. i am unable to think of anything. please help.

I have used a code that scans each line and says..if its fine or some problem.

Code:
awk '{
  print ($4 == 0 ?  "fine" : "some problem")
    }'  inputfile

I hope, m clear at my point.
# 2  
Old 01-10-2011
Well, it is an edge case, being last. Your thinking is too procedural too soon. Assuming spaces as column separators, then logically, the test is, can you find a line not ending in space-zero? If they are all good, you have to look at all, but if any is bad, that is all you need to see, which is the possibility of less work:
Code:
if grep -vq ' 0$' inputfile
then
 echo "some problem"
else
 echo "everything is fine"
fi

In awk, the procedure is to 1) find the first bad, then print bad message and exit, and 2) at last line (if you survive), print good message. i am not an awk fan, I use sed, and do it as a data transformation:
Code:
sed '
  s/.* [^0]$/some problem/
  t x
  ${
    s/.*/everything is fine/
    t
   }
  d
  :x
  q
 ' inputfile


Last edited by DGPickett; 01-10-2011 at 02:49 PM..
# 3  
Old 01-10-2011
Code:
awk '$4{print "Some Problem"; e=1;exit 1}END{if(!e)print "fine"}' infile

# 4  
Old 01-11-2011
If you exit, how will you ever test e?
# 5  
Old 01-11-2011
Using exit in awk will take you to the END Smilie

Code:
$ awk 'BEGIN { e = 1; exit } END { print e }'
1

# 6  
Old 01-11-2011
Oh, a little exit! That's not making me like awk any better! My sed needed no variable, as bad record processing does a real exit!
# 7  
Old 01-11-2011
Quote:
Originally Posted by Scrutinizer
Code:
awk '$4{print "Some Problem"; e=1;exit 1}END{if(!e)print "fine"}' infile

Newbie question.. Smilie
How do you interpret this code?
awk , read column 4, then print "some problem".
But what about checking if the value is zero? Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scanning a pdf file in Linux shell

I want to search a keyword in a list of pdf files and when i find a match i want to write the title and author of that pdf file to another file. How will I do this using linux shell script? (7 Replies)
Discussion started by: SK33
7 Replies

2. Shell Programming and Scripting

Reg scanning time based log file

Hi, I have a requirement to scan Oracle's alert log file. This file logs all event for Oracle database and each line will have timestamp followed by messages (which might be one or more lines). Example. Thu Aug 15 17:35:59 2013 VKTM detected a time drift. Please check trace file for more... (1 Reply)
Discussion started by: manickaraja
1 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

5. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

6. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

7. Shell Programming and Scripting

To cut entire column from a file and apend it to another file as another column

file1.txt : india pakistan bangladesh japan canada africa USA srilanka Nepal file2.txt Delhi Tokyo washington I have to cut the first column of file1.txt and apend it with file2.txt as another column like this Delhi india Tokyo japan washington USA ... (4 Replies)
Discussion started by: sakthifire
4 Replies

8. UNIX for Advanced & Expert Users

Scanning file backwards

Is there any way to look for a directory path that is listed any number of lines *before* a keyword in an error message? I have a script that is trying to process different files that are always down a certain portion of a path, and if there is an error, then says there is an error, contact... (2 Replies)
Discussion started by: tekster757
2 Replies

9. Shell Programming and Scripting

How to zip a modified file 15 days before but not scanning my sub directory files

I am using zip -m option to zip my files, but i dont want my sub directories files to be zipped (1 Reply)
Discussion started by: skrish70
1 Replies

10. Shell Programming and Scripting

scanning for '0' value in .txt file

Hello I am a novice shell scripting programmer, so please bare with me. I have embedded a simple SQL statement into a shell script, which simply returns an integer (its a count (*) statement). The result of the statement is then oputput to .txt file. So, the number could be 0, 1,2, 10,... (4 Replies)
Discussion started by: man80
4 Replies
Login or Register to Ask a Question