Visit Our UNIX and Linux User Community


Awk , Sed Print last 4 numeric characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk , Sed Print last 4 numeric characters
# 1  
Old 10-30-2009
Awk , Sed Print last 4 numeric characters

Hello All,
I have been searching and trying this for a bit now. Can use some assistance.

Large 5000 line flat file.
bash, rhel5

Code:
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.

Code:
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:

Code:
[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
# 2  
Old 10-30-2009
This should work:

Code:
awk '$NF > 99 && $NF < 10000' file > newfile

# 3  
Old 10-30-2009
Code:
grep '[0-9]\{3,\} *$' infile

# 4  
Old 10-30-2009
Or a sed option:
Code:
$ 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

# 5  
Old 10-30-2009
Amazing,

I will try to explain. Please correct me if I am wrong.

1. If the number of fields in the current record are less than 99.

2. Pass command only if first command exits with 0.

3. If the number of fields in the current record are less than 1000. Direct output to newfile

awk '$NF > 99 && $NF < 10000' file
# 6  
Old 10-30-2009
Nop, $NF is the value of last field.
# 7  
Old 10-30-2009
Code:
awk '$NF > 99 && $NF < 10000' file

Simply, if the last field ($NF) is greater then 99 and less then 10000 print the line.

Previous Thread | Next Thread
Test Your Knowledge in Computers #994
Difficulty: Medium
A hypervisor or virtual machine monitor (VMM) is a computer software, firmware or hardware that creates and runs virtual machines.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print column number while ignoring alpha characters

I have the following script that will print column 4 ("25") when column 1 contains "123". However, I need to ignore the alpha characters that are contained in the input file. If I were to ignore the characters my output would be column 3. What is the best way to print my column of interest... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

2. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

3. Shell Programming and Scripting

Use awk to print first 6 characters

Hi, i want to use awk to print the first 6 characters of a variable awk -F"|" '$3>0 { print $3 }' z00.unl > z001.unl but $3= 7 digits and i just want to print the first 6 digits. eg 1005779 but i want to print only 100577 (3 Replies)
Discussion started by: dealerso
3 Replies

4. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

5. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

6. Shell Programming and Scripting

awk and sed, how to exclude certain characters

Hello everyone: I have ran into this a few times now where my skills are just not up to snuff when it comes to Unix. So, I came here to find some beard stroking Unix wizard to help me. Basically, I am using OS X 10.5 in large scale at work and sometimes I have to run some custom reports. ... (5 Replies)
Discussion started by: tlarkin
5 Replies

7. Shell Programming and Scripting

Extract some characters with SED or AWK

Hi, I have the following example string: today_is_a_good_day.txt The character "_" inside the string can sometimes be more or less. The solution for every string equal the count of "_" should be alway the rest after the last underline character. Result: day.txt I want to use awk... (5 Replies)
Discussion started by: climber
5 Replies

8. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies

9. Solaris

Password without numeric characters

G'day guys, Just a simple question: Is it possible to set user's passwords without numeric characters? I prefer to have passwords as simple words, but when going through SMC, i get an error that the first six characters must contain at least 2 alphabetic and 1 numeric. Server is a Sun... (3 Replies)
Discussion started by: drchris
3 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies

Featured Tech Videos