avoid open file to check field.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting avoid open file to check field.
# 1  
Old 10-30-2009
avoid open file to check field.

Hi Everyone,

Code:
[root@]# cat a.txt
94,aqqc,62345907,
5,aeec,77,

[root@]# cat 1.pl
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;
open(my $FA, "/root/a.txt") or die "$!";
while(<$FA>) {
        chomp;
        my @tmp=split(/\,/, $_);
        if (index($tmp[1], "qq") ne -1) {
                print "$_\n";
        }
}
close ($FA);

[root@]# perl 1.pl
94,aqqc,62345907,

As we can see, if i want to print out this a.txt, but without the line field 1 value contains "qq", i can use open the file method.

Wonderring if there is any more simple way to do that, more efficient one, image i have 1000lines for the a.txt? Smilie

Thanks
# 2  
Old 10-30-2009
Can you provide a better sample of your input file?
I don't see any "qq" in field 1, assuming the fieldseparator is a comma.
# 3  
Old 10-30-2009
Quote:
Originally Posted by Franklin52
Can you provide a better sample of your input file?
I don't see any "qq" in field 1, assuming the fieldseparator is a comma.
Hi Frank,

the file is:
94,aqqc,62345907,
5,aeec,77,

you can see "qq" is part of line 1, field 1 "aqqc", means when field 1 contains "qq" this word, then print it out.

Thanks
# 4  
Old 10-30-2009
As I can see "aqqc" is the second field if the fieldseparator is a comma, anyway with awk you can do something like:

Code:
$ awk -F, '($2 ~ /qq/)' file

# 5  
Old 10-30-2009
MySQL

Quote:
Originally Posted by Franklin52
As I can see "aqqc" is the second field if the fieldseparator is a comma, anyway with awk you can do something like:

Code:
$ awk -F, '($2 ~ /qq/)' file

Smilie That's amazing, it works, !~ is reverse way. Smilie
Thanks Frank, it saves a lot of time compare with my method, to open the file, index grep that value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check field value from a file records

I need a script to check the records in a file , if any value match transfer the record in error.txt file. 1- If any of the any field value is NULL(nothing in this field) Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Example: 11111111|22222222|NULL|12|444|27042018|27042018... (8 Replies)
Discussion started by: vivekn
8 Replies

2. UNIX for Beginners Questions & Answers

How to check if a file is open in editor?

Hi there! I'm developing a program that allows the user to open and edit files using both an editor and the terminal. Once the user has finished editing the file an update is sent to the logbook that compares the file before and after it was edited - this can only be done if the file is closed (I... (23 Replies)
Discussion started by: cherryTango
23 Replies

3. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

4. Shell Programming and Scripting

Field value check

used below command to redirect records having P000 in all columns except 4th one in the below files with 3 records , lets say outout in below file is record 1,2 not 3 which is having P000 in the 4th column below commad not working , any help ? awk 'NF>0 { for(i=11;i<=NF;i+=1) if (i!=4 &&... (7 Replies)
Discussion started by: airesh
7 Replies

5. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

6. Shell Programming and Scripting

How to check field formatting of input file?

Hi, I had input file with below data, abcdefghij;20100903040607;1234567891;GLOBAL; Having values of fields with seperated by semi-colon (;) and ended with line feed (\n). Through shell script, how can I check the field formatting? Thanks in advance. (18 Replies)
Discussion started by: Poonamol
18 Replies

7. Shell Programming and Scripting

simplify the script, check field match to value in a file

Hi Everyone, Below is the script, i feel there should be more simple way to do the same output, my one works, but feel not nice. like using index i feel it is slow (image my file is very large), maybe awk can do one line code? Please advice. # cat 1.txt 1 a 2 b 3 cc 4 d # cat 1.pl... (6 Replies)
Discussion started by: jimmy_y
6 Replies

8. Shell Programming and Scripting

check for the value of one particular field and give output in a different file

hi i need to check for the value of one particular field : in the output file. the file may contain many such records as below how to ???? *** Throttled with base name + key params! : : -518594328 : les.alarm.LBS12005 : les.alarm.LBS12005 : les : lessrv1 : les : 2328 : 0... (7 Replies)
Discussion started by: aemunathan
7 Replies

9. UNIX for Dummies Questions & Answers

check for "cannot open file"

I have a small script that checks to see if a file exists and that it has data in it. I also need to check if the file can be opened. I had an issue today where the file it checks could not be opened and my script did not catch it. How do I check to see if it cannot be opened? ... (19 Replies)
Discussion started by: ssmith001
19 Replies

10. Solaris

How to check no. of files open currently

I'm getting an error "too many files open" # ulimit -a time(seconds) unlimited file(blocks) unlimited data(kbytes) unlimited stack(kbytes) 8192 coredump(blocks) unlimited nofiles(descriptors) 256 memory(kbytes) unlimited # hard limit shows 1024 I would like to know how many files... (1 Reply)
Discussion started by: max_min
1 Replies
Login or Register to Ask a Question