extract part of string using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract part of string using sed
# 1  
Old 07-12-2012
extract part of string using sed

Hi,

I have the following string and I need to extract the date from it:

TextHere,2012/07/11,1

I tried using sed:
Code:
sed -n 's#^.*\([0-9]{4}/[0-9]{2}/[0-9]{2}\).*$#\1#p'

But it doesn't return anything. The following line doesn't even return '2012':
Code:
sed -n 's/^.*\([0-9]{4}\).*$/\1/p'

I used to use grep -o for this, but it's not supported on this system unfortunately.
# 2  
Old 07-12-2012
Hi


Code:
$ echo TextHere,2012/07/11,1 | awk -F, '{print $2}'
2012/07/11

# 3  
Old 07-12-2012
Quote:
Originally Posted by guruprasadpr
Hi


Code:
$ echo TextHere,2012/07/11,1 | awk -F, '{print $2}'
2012/07/11

Thanks, this is not sufficient unfortunately. The string doesn't always look like this so I need some way to just match the date.
# 4  
Old 07-12-2012
Code:
# echo 'TextHere,2012/07/11,1' | sed 's#^.*\([0-9]\{4\}/[0-9]\{2\}/[0-9]\{2\}\).*$#\1#'
2012/07/11

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 07-12-2012
Hi

Code:
sed -n 's#^.*,\([0-9]\{4\}/[0-9]\{2\}/[0-9]\{2\}\).*$#\1#p'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 6  
Old 07-12-2012
Alternatively, you can use grep too
Code:
echo 'TextHere,2012/07/11,1' | grep -oE '[0-9]{4}/[0-9]{2}/[0-9]{2}'

# 7  
Old 07-12-2012
Thanks, that's it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract part of string from all log files.?

Hi All, Let us say we have 5 log files, extract the data from all log files and save the output in a file. home/log/first.log home/log/second.log home/log/third.log home/log/four.log home/log/five.log I want to extract the following text from the log files and save the output in a file.... (7 Replies)
Discussion started by: ROCK_PLSQL
7 Replies

2. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

3. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Extract part of a string

I have a file with 100s of lines of text. I want to perform an extraction of this line: Info bpzs(pid=2652) using 1000 bits I have not been able to extract it. Should I try expr match or is there another method? This line has data both in front of and in back of it. I do not have grep -o... (5 Replies)
Discussion started by: newbie2010
5 Replies

5. Shell Programming and Scripting

Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string " This has been entered in MS_RECENT_ACTIVITY the... (2 Replies)
Discussion started by: simpletech369
2 Replies

6. Shell Programming and Scripting

SED - replace only on part of the string

Hello there, I need some help. I have a file containing this : $ cat file PARM1=(VAL11),PARM2=(VAL21,VAL22,VAL23),PARM3=(VAL31),PARM4=(VAL41,VAL42) and I need to replace all the ',' by '|' but only those which are between brackets. Output would be :... (10 Replies)
Discussion started by: Sephiburp
10 Replies

7. Shell Programming and Scripting

extract last part of string.

Hi, I have a string like this BUNDLE=/home/bob/flx/user.bun how to extract only the the last part ie, only user.bun (2 Replies)
Discussion started by: vprasads
2 Replies

8. Shell Programming and Scripting

[sed] extract words from a string

Hi, One of the scripts creates logs in the format: progname_file1.log.20100312020657 where after file the number could be from 1 to 28 and after log. the date is attached in the format YYYYMMDDHHMISS progname_file<1-28>.log.YYYYMMDDHHMISS. Now I want to discard the .20100312020657... (7 Replies)
Discussion started by: dips_ag
7 Replies

9. Shell Programming and Scripting

Extract Part of string from 3rd field $3 using AWK

I'm executing "wc -lc" command in a c shell script to get record count and byte counts and writing them to a file. I get the result with the full pathname of the file. But I do not want the path name to be printed in the output file. I heard that using Awk we can get this but I don't have any... (4 Replies)
Discussion started by: stakuri
4 Replies

10. Shell Programming and Scripting

using sed to replace a part of string

Hi, I have files that are named front1.txt to front999.txt. They are all in the same directory. To change "front" to "back", I am doing something like this. for file in *.txt; do new=`echo $file | sed 's/^**/back/g'` mv $file $new done My problem is what if files are named... (6 Replies)
Discussion started by: csejl
6 Replies
Login or Register to Ask a Question