extract string until regexp from backside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract string until regexp from backside
# 1  
Old 05-26-2010
extract string until regexp from backside

Hi,

I searched in the forums, but I didn't find a good solution. My problem is:
I have a string like "TEST.ABC201005.MONTHLY.D101010203".
I just want to have the string until the D100430, so that the string should look like: "TEST.ABC201005.MONTHLY.D"
The last characters after the D can be 6-10 characters long, so that sometimes D101010 is the last part of the string, but the next day D10301006.

Does anyone know a handy sed?

I tried: echo $string |sed 's/.\{6\}$//g'
# 2  
Old 05-26-2010
Hi,
if then number of fields are always 4 (with . as field separator) maybe this would be a solution:

Code:
echo $string | awk 'BEGIN{FS="."} {print $1"."$2"."$3".D"}'

if the number of fields are variable, then use a for loop until NF-1 (number of fields.
This User Gave Thanks to albertogarcia For This Post:
# 3  
Old 05-26-2010
Try:
Code:
echo $string | sed 's/\(.*\..\).*/\1/'

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-26-2010
Quote:
Originally Posted by Franklin52
Try:
Code:
echo $string | sed 's/\(.*\..\).*/\1/'

hmm..can you explain this ..looks like chinese for me Smilie
# 5  
Old 05-26-2010
SED sol:
Code:
echo "TEST.ABC201005.MONTHLY.D101010090900203"|sed -e "s/\.D[^\.]\{1,\}/\.D/"

This User Gave Thanks to Klashxx For This Post:
# 6  
Old 05-26-2010
Quote:
Originally Posted by albertogarcia
Hi,
if then number of fields are always 4 (with . as field separator) maybe this would be a solution:

Code:
echo $string | awk 'BEGIN{FS="."} {print $1"."$2"."$3".D"}'

if the number of fields are variable, then use a for loop until NF-1 (number of fields.

I tried this:
echo $string |awk -F. '{print $1"."$2"."$3".D"}' - I think this is the same Smilie

---------- Post updated at 11:55 AM ---------- Previous update was at 11:53 AM ----------

Quote:
Originally Posted by Klashxx
SED sol:
Code:
echo "TEST.ABC201005.MONTHLY.D101010090900203"|sed -e "s/\.D[^\.]\{1,\}/\.D/"


It looks like I have to learn a new language - sed. Very confusing.
# 7  
Old 05-26-2010
Quote:
Originally Posted by elifchen
hmm..can you explain this ..looks like chinese for me Smilie
Have a read of Sed - An Introduction and Tutorial.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Regexp for string that might contain a given character

I'm probably just not thinking of the correct term to search for :-) But I want to match a pattern that might be 'ABC' or '1ABC' there might be three characters, or there might be four, but if there are four, the first has to be 1 (1 Reply)
Discussion started by: jnojr
1 Replies

3. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Shell Programming and Scripting

How to use regexp to find an ipaddress from a query string?

I need help with a regexp to find out the ip address which can possibly be present in a URL. The URLs can be in any of the following form <domain>?a=12345&d=somestring1 <domain>?c=10.10.10.100&d=somestring1 <domain>?a=12345&b=somestring1&c=10.1.2.4d=somestring2... (3 Replies)
Discussion started by: ampak
3 Replies

5. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. UNIX for Dummies Questions & Answers

regexp: match string that contains list of chars

Hi, I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out. If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep: echo $STRING|grep a|grep b|grep c but, how would you do that in a single... (9 Replies)
Discussion started by: jimcanoa
9 Replies

7. Shell Programming and Scripting

...yet another string of awk/sed questions from a RegExp-Challenged luser %-\

Greetings all, ...here is yet another string of awk/sed questions from a RegExp-Challenged luser :eek: I'm looking to have sed/awk do some clean-up on routing tables and to that end, I would like to do the following: 1.) If a line contains the word "masks" or "subnets" prepend CR/LF to... (16 Replies)
Discussion started by: SteveB-in-LV
16 Replies

8. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

9. Shell Programming and Scripting

Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here: There is this text file with text as: Line one has a bingo Line two does not have a bingo but it has a tango Bingo is on line three Line four has both tango and bingo Now I would want to search for the pattern "bingo" in this file... (3 Replies)
Discussion started by: manthasirisha
3 Replies

10. Shell Programming and Scripting

regexp to get first line of string

Hi everybody for file in * #Bash performs filename expansion #+ on expressions that globbing recognizes. do output="`grep -n "$1" "$file"`" echo "$file: `expr "$output" : '\(^.*$\)'`" done In the above bash script segment, I try to print just the first line of string named... (3 Replies)
Discussion started by: jonas.gabriel
3 Replies
Login or Register to Ask a Question