print remaining file after a condition is satisfied


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print remaining file after a condition is satisfied
# 1  
Old 05-07-2007
print remaining file after a condition is satisfied

Hi ,

could any one suggest me that how to determine if the first field is numeric and if it is greater than another number then from that point everything else should be printed using awk.

I have tried this
:

Code:
awk  -v xxxx=$xxxxx '
BEGIN {
enable=0
}
{
print $1
if ( ( $1 !~ "^[0-9][0-9]*$"  || ( $1 ~ "^[0-9][0-9]*$" && $1 <  xxxx ) )  )
{  continue }
else
enable=1

if ( enable == 1)
print $0
 }' res >res1

# 2  
Old 05-07-2007
Code:
awk -v enable=0 -v xxxx=$xxxx ' enable != 1 && $1 ~ "^[0-9][0-9]*$" && $1 > xxxx { enable=1} enable ' file

# 3  
Old 05-07-2007
Thanks, It is working fine from command line, but when i put it in a script it gives me this error and also, could you please let me know the last "enable" why have you used it????

Quote:
awk: Cannot find or open file 3284
3284 is the value of the variable $xxxx
# 4  
Old 05-07-2007
Quote:
Thanks, It is working fine from command line, but when i put it in a script it gives me this error and also, could you please let me know the last "enable" why have you used it????
Quote:
awk: Cannot find or open file 3284
3284 is the value of the variable $xxxx
The awk command is valid.
Show us your script.


Another way to do the work with awk :
Code:
awk -v xxxx=$xxxx ' ($1 ~ "^[0-9][0-9]*$" && $1 > xxxx),0 ' file


Jean-Pierre.
# 5  
Old 05-07-2007
Jean-peirre,

sorry, but your solution doesnt work. it is printing all the lines. Anbu's solution worked fine. regarding the error, I was passing incorrect parameter. Now i have managed to solve it. Anyways, Do you mind explaining me your solution and Anbu's solution????
# 6  
Old 05-07-2007
I am working on AIX, and the command works fine :
Code:
$ cat file
AAAAA Not printed
099   Not printed
101   Printed 1
102   Printed 2
XXX   Printed 3 and last
$ xxxx=100
$ awk -v xxxx=$xxxx ' ($1 ~ "^[0-9][0-9]*$" && $1 > xxxx),0 ' file
101   Printed 1
102   Printed 2
XXX   Printed 3 and last
$

When the pattern in in the form of pattern1,pattern2, action is performed on every record starting whith the record that matches the first pattern pattern1, and continuing throught and including the record that matches the second pattern pattern2.

pattern1 -> ($1 ~ "^[0-9][0-9]*$" && $1 > xxxx)
pattern2 -> 0 Is equivalent To 'End Of File' since 0 is considered as false by awk.



Code:
awk -v enable=0 -v xxxx=$xxxx ' enable != 1 && $1 ~ "^[0-9][0-9]*$" && $1 > xxxx { enable=1} enable ' file

enable != 1 && $1 ~ "^[0-9][0-9]*$" && $1 > xxxx { enable=1}
If a valid record have not been found (enable!=1) and if the current record is valid (tests on field $1), memorize valid record found (enable=1) so from this point all records must be printed.

enable
Test if a valid record have been found (false if enable==0, true otherwise).
The default action is 'print $0'

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: duplicate column and print remaining as is

Hello there I'd like to make a copy of 2nd column and have it printed in place of column 1. Remaining columns are needed as it. test data: ProbeSet GeneSymbol X22565285 X22566285 ILMN_1050008 MYOCD 6.577 7.395 ILMN_1050014 GPRC6A 6.595 6.668 ILMN_1050017 ... (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

Print remaining lines using grep

Hi All, I am having a text file like below ERROR - Not a valid ID : 123 ERROR - Not a valid hello ID : 124 SUCCESS - Valid ID : 12 I need to display like below after reading the file if it finds the error keyword along with displaying this first line when error pattern... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

3. Shell Programming and Scripting

If both condition satisfied

pattern tin loop 3 tin xarg 1 tin jim 5 tin icon 2 tin tour 4 patn.out tin loop 3 tin jim 5 tin icon 1 tin tour 2 while read one two three do awk '$2 ~ /'"$two"'/' patn.out || while read four five six do awk -v n1=$three -v n2=$six '{BEGIN (n2<n1)?1:0}' done<patn.out... (3 Replies)
Discussion started by: Roozo
3 Replies

4. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

5. Shell Programming and Scripting

Awk to print on condition in input file

I need only those records which has $2 equal to "DEF" independent of case (i.e upper or lower) nawk -F"," '$2 ~ //{print $0}' file This returns 3rd record also which i dont want I tried this but this doesnt work as expected. nawk -F"," '$2 == ""{print $0}' file i dont... (3 Replies)
Discussion started by: pinnacle
3 Replies

6. UNIX for Dummies Questions & Answers

print remaining part from the first-match within a file

Hi, i was looking for unix command(s) for : find the first occurrence of a given pattern with in a file and print the remaining part. below is an example of what i am looking for: lets say, a file named myfile.txt now, the command i am looking for will do the following (4 Replies)
Discussion started by: nurulamin862
4 Replies

7. UNIX for Dummies Questions & Answers

until loop : running even if condition is satisfied

I am checking one until loop with the following logic until || |||| do sleep 30 done before running this i am exporting above variables to SUCCESS.But the loop is running in infinite loop.My requirement is to make the loop run until all the 3 variables are SUCCESS or any of the 3... (2 Replies)
Discussion started by: dr46014
2 Replies

8. Shell Programming and Scripting

Insert first line of a file to first column of remaining files

I want to extraxt data from a html table the html file is downloaded from UG / PG Univ - Exam.Results April/May 2008 After processing the html file using sed i got the output like this 11305106082,RANJANI R, CS1251,20,69,P CS1302,20,45,P EC1006,20,52,P EC1351,20,53,P... (5 Replies)
Discussion started by: a_artha
5 Replies

9. UNIX for Dummies Questions & Answers

Changing old file name to new one if the old file name satisfied certain condition

Hi guys, I desperately need your help on my problem:- I got a folder in my directory that has many files in it. Most of the file's name is starting with certain prefix. Eg: ABC, CDE, EFG, etc. I only want to change file name that is starting from certain prefix and the rest I want them to... (1 Reply)
Discussion started by: balzzz
1 Replies

10. Shell Programming and Scripting

how to create folder wen the condition satisfied

hi i hav files ha1j ha2m ha3n ha4q ha5s ...like tat im having some 20 files ..and i want to create a folder as the same amount of files which im having wen the condition if loop is satisfied .. thank you (5 Replies)
Discussion started by: maximas
5 Replies
Login or Register to Ask a Question