Pattern Search not working properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Search not working properly
# 1  
Old 04-09-2014
Pattern Search not working properly

Hi

I have a code as below.

Code:
vDate=`echo $filename | rev | sed 's/.*\([0-9]\)\..*//g' | rev`

Basically I am trying extract timestamp from files with formats as below.

Code:
test.input.20130501010101.txt
test.input.crm.201405010202.dat

The code was working fine, but when I don't have a timestamp appending, the above code is giving entire file name instead of empty string
i.e.
test.input.txt is returning test.input.txt, where as I am searching for numeric value.

Last edited by Scrutinizer; 04-09-2014 at 05:36 AM..
# 2  
Old 04-09-2014
Hello,

Following may help you to get only timestamp.

Code:
echo "test.input.20130501010101.txt" | awk 'gsub(/[[:alpha:]]/,X) gsub(/[[:punct:]]/,Y) 1'

Output will be as follows.

Code:
20130501010101


Thanks,
R. Singh
# 3  
Old 04-09-2014
Code:
vDate=`echo $filename | sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/gp'`

This User Gave Thanks to anbu23 For This Post:
# 4  
Old 04-09-2014
Shell without external utilities (fast):
Code:
vdate=${filename%.*}
vdate=${vdate##*[!0-9]}

awk version:
Code:
vdate=$(echo "$filename" | awk -F. '{$0=$(NF-1)}/^[0-9]+$/')



---
@Ravindersingh: That works with the given input, but it would be unreliable, since it would render the wrong result if there is so much as a single digit anywhere else in the filename.
# 5  
Old 04-09-2014
Using grep, assuming that the timestamp is 12+ characters, and there are no other 12+ digit numbers in the filename:
Code:
echo test.input.20130501010101.txt | grep -o '[0-9]\{12,\}*'

# 6  
Old 04-13-2014
Hi anbu23

Code:
vDate=`echo $filename | sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/gp'`

can you please explain me the [0-9]{1,\} with the "p" option and why was my code not working.
# 7  
Old 04-14-2014
Quote:
-n Suppress the default output; sed displays only those lines specified with the p command or with the p flag of the s command.
You are matching only single digit. Use pattern
Quote:
[0-9]\{1,\}
to match minimum one digit or two digits or.. n digits.

Code:
echo $filename | rev | sed 's/.*\.\([0-9]\{1,\}\)\..*/\1/g' | rev

Code:
$ filename=test.input.20130501010101.txt
$ echo $filename | rev | sed 's/.*\([0-9]\)\..*/\1/g' | rev
2
$ echo $filename | rev | sed 's/.*\([0-9]\{1,\}\)\..*/\1/g' | rev
2
$ echo $filename | rev | sed 's/.*\.\([0-9]\{1,\}\)\..*/\1/g' | rev
20130501010101

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Expansion not working properly

I'm using an Ubuntu machine and expansion is not working properly. What would cause this? Do I need to check for any particular bash packages? $ ipcs -m | grep $USER | awk '{printf "%s ",$2}' $ ipcs -m | grep UNF | awk '{printf "%s ",$2}' 294912 1048577 425986 688131 786436 1245189... (14 Replies)
Discussion started by: cokedude
14 Replies

3. Shell Programming and Scripting

Join not working properly

I want to join two files , with file 1 col 3 and file 2 col 1 as key. The join command is erratic for some reason. File 2 is a master file having all the names, and file 1 has some values. I want to add the names from fil2 in file 1. If I use the original master file, some output is missing. ... (16 Replies)
Discussion started by: ritakadm
16 Replies

4. UNIX for Dummies Questions & Answers

~c is not working properly with -r option

Hi There, --------- file1 ------- ~c asd@ac.com -------------- Now i am using below command cat file1|mailx -s " testing" -r " My Name" abc@tech.com (3 Replies)
Discussion started by: Tapan Sharma
3 Replies

5. Linux

rexec not working properly

Hi, I am trying to enable rexec to automate certain tasks(it has to be rexec, not ssh or any other due to the system environment), so after switching to linux, I followed the certain instructions that were laid out in the web. My operating system is fedora 17, so I first installed the... (1 Reply)
Discussion started by: wringer
1 Replies

6. Shell Programming and Scripting

mailx not working properly

I am using mailx command in my script to attach a file and send an email. I need to attach a csv file and send email to a mail id - I am using uuencode output.csv output.csv | mailx -s "test mail" xyz@abc.com This will send a mail with scrambled text in body. am i missing something ?... (4 Replies)
Discussion started by: Sriranga
4 Replies

7. Shell Programming and Scripting

\n not working properly

Hi all, I'm trying to generate a series of txt files starting from a plain csv file part of my code: #!/bin/ksh INSTALLDIR=/Users/ME/Installdir CSV=CSV.csv TMP=/tmp/$(basename $0).txt tr -s "\r" "\n" < /$INSTALLDIR/$CSV > $TMP function Makefiles { printf '%24s:%30s\n' "sometext"... (1 Reply)
Discussion started by: Jive Spector
1 Replies

8. HP-UX

FC card not working properly

Hi I've a problem with Hp-ux 11.11 9000/800/rp3440 system. Already the software for driver & its patch are loaded for HBA Fibrechannel card, but still the fibrechannel card is showing the status "Unclaimed" . What will be reason for this? How to get the status "Claimed" ? Pl. help me out.... (4 Replies)
Discussion started by: Mike1234
4 Replies

9. Programming

y is this not working properly?

#include <stdio.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct stat s; main() { char c; if (fork()==0) { system("clear"); do { printf("myAI\\>§ "); scanf("%s",c); if(stat(c,&s)>-1) {... (3 Replies)
Discussion started by: C|[anti-trust]
3 Replies

10. UNIX for Dummies Questions & Answers

Keyboard not working properly...

Hello Again, Those that have noticed my earlier posts will know that I have succesfully installed Solaris 8 onto my pc. I haven't been able to get x-server working (i think it doesn't like my video card) though I've been able to log into root (with a bit of help from unix forums :o ) and have... (2 Replies)
Discussion started by: timresh
2 Replies
Login or Register to Ask a Question