(i) Print the line if the number of fields is less than 3
(ii) If the 3rd field exists, print the line if it does not consist of any digits. Its length does not matter here.
(iii) If the 3rd field exists, print the line if it consists ONLY of digits and has a length of 5 or less.
(iv) There can be more than 3 fields, and the line will be printed as long as rules (ii) and (iii) are satisfied.
So for the data below, you want to display lines 1,2,4 and 7:
$
$ cat -n data.txt
1 field 12345 12345 a
2 field 12345 a
3 field 12345 123456 a
4 field 12345
5 field 12345 12aQ~
6 field 12345 12aQ~ 123
7 field 12345 1340 4578 abc AB#xy2
$
$
Perl script:
$
$ perl -ne '@a=split; $f=$a[2]; ($x=$f)=~s/\d//g; ($y=$f)=~s/\D//g;
> print if ( $#a<2 or
> ($#a>=2 and $x eq $f) or
> ($#a>=2 and $y eq $f and length($f)<=5)
> )' data.txt
field 12345 12345 a
field 12345 a
field 12345
field 12345 1340 4578 abc AB#xy2
$
$
Thank�s man.
But can�t you do this somehow with grep and reg. expressions? I have checked man for grep and I see speciall characters like "?" could be helpfull, but I don�t know how to used it.
But thanks tyler_durden, your solution in perl does the trick in this case.
cat f
field 12345 12345 a
field 12345 a
field 12345 123456 a
field 12345 12345
grep "^field [0-9]\{1,5\}\( [0-9]\{1,5\}\|\) [a-z]$" f
field 12345 12345 a
field 12345 a
Hi,
I've got a file which different lines. There are an sql sentence in each one. It looks like this:
alter table bla bla
insert into bla bla
select bla bla
alter table
I'd like the output to show the matches in the same order as they are in the original file. I think that could be done with the "or" or using regexp.
That is, something like this:
grep "insert into" || "alter table" archivo
So it would show the line either with "insert into" or "alter table", the first matching line
Maybe this is done with regexp, I havent used them much and cant find the way to solve this.