abacus
October 30, 2009, 10:01am
1
Hello All,
I have been searching and trying this for a bit now. Can use some assistance.
Large 5000 line flat file.
bash, rhel5
Input File Sinppet:
Fri Oct 30 09:24:02 EDT 2009 -- 1030
Fri Oct 30 09:26:01 EDT 2009 -- 73
Fri Oct 30 09:28:01 EDT 2009 -- 1220
Fri Oct 30 09:30:01 EDT 2009 -- 111
Fri Oct 30 09:32:01 EDT 2009 -- 56
Fri Oct 30 09:34:01 EDT 2009 -- 1136
Fri Oct 30 09:36:01 EDT 2009 -- 84
Fri Oct 30 09:38:01 EDT 2009 -- 67
Fri Oct 30 09:40:02 EDT 2009 -- 1145
Fri Oct 30 09:42:01 EDT 2009 -- 85
Fri Oct 30 09:44:01 EDT 2009 -- 63
Fri Oct 30 09:46:01 EDT 2009 -- 115
Fri Oct 30 09:48:01 EDT 2009 -- 72
I would like to redirect output to file in the following format. Basically if the tail number contains 3 or 4 characters, output it to the file.
Desired Output
Fri Oct 30 09:24:02 EDT 2009 -- 1030
Fri Oct 30 09:28:01 EDT 2009 -- 1220
Fri Oct 30 09:30:01 EDT 2009 -- 111
Fri Oct 30 09:34:01 EDT 2009 -- 1136
Fri Oct 30 09:40:02 EDT 2009 -- 1145
Fri Oct 30 09:46:01 EDT 2009 -- 115
I have been playing with AWK, SED GREP and CUT. My research is pointing me to something like this:
[root@db3 ~]# awk '{ print substr( $0, length($0) - 4, length($0) ) }' file > newfile
1030
- 73
1220
111
- 56
1136
- 84
- 67
1145
- 85
- 63
115
-- 72
However it's still giving the 2 number strings. And the date is not getting printed.
Please Advise.
Regards,
Bacus
This should work:
awk '$NF > 99 && $NF < 10000' file > newfile
grep '[0-9]\{3,\} *$' infile
Scott
October 30, 2009, 10:13am
4
Or a sed option:
$ sed -n "/[0-9]\{3,4\} *$/p" file1
Fri Oct 30 09:24:02 EDT 2009 -- 1030
Fri Oct 30 09:28:01 EDT 2009 -- 1220
Fri Oct 30 09:30:01 EDT 2009 -- 111
Fri Oct 30 09:34:01 EDT 2009 -- 1136
Fri Oct 30 09:40:02 EDT 2009 -- 1145
Fri Oct 30 09:46:01 EDT 2009 -- 115
abacus
October 30, 2009, 10:18am
5
Amazing,
I will try to explain. Please correct me if I am wrong.
If the number of fields in the current record are less than 99.
Pass command only if first command exits with 0.
If the number of fields in the current record are less than 1000. Direct output to newfile
awk '$NF > 99 && $NF < 10000' file
danmero
October 30, 2009, 10:32am
6
Nop, $NF is the value of last field.
awk '$NF > 99 && $NF < 10000' file
Simply, if the last field ($NF) is greater then 99 and less then 10000 print the line.
awk -F"-" '$3>=100 && $3<=9999' a.txt
abacus
November 1, 2009, 10:04am
9
Hello,
@danmero
@franklin52
From awk manpage:
NF number of fields in the current record
I am not seeing any where in the man page that $NF is value of last field. I am reading man page on:
Darwin Kernel Version 10.0.0 osx Snow Leopard.
Is NF number of fields in the current record as well?
Abacus
Note that NF and $NF are different.
NF is what you read in the man pages, $NF expands to the value of the last field (because of the leading $).
$1 is the first field, $number_of_fields is the last one.
Hope this helps.