Extract some characters with SED or AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract some characters with SED or AWK
# 1  
Old 08-26-2008
Question 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 or sed inside a shell script. Any ideas? Smilie

Thanks
climber
# 2  
Old 08-26-2008
echo "today_is_a_good_day.txt" | awk -F"_" '{ print $NF }'
# 3  
Old 08-26-2008
With sed:

Code:
sed 's/.*_\(.*\)/\1/'

Regards
# 4  
Old 08-26-2008
Thanks for your really quick answers :-)

climber
# 5  
Old 08-26-2008
If the string is inside a variable, you can also do :
Code:
> var=today_is_a_good_day.txt
> echo ${var##*_}
day.txt
>

Jean-Pierre.
# 6  
Old 08-26-2008
Code:
# s=today_is_a_good_day.txt
# IFS=_
# set -- $s
# eval echo \$$#
day.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

Deleting characters with sed,perl,awk

Input: :: gstreamer :: xine-lib :: xine-lib-extras Output should be: gstreamer xine-lib xine-lib-extras How can it be done with sed or perl? (12 Replies)
Discussion started by: cola
12 Replies

3. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

4. 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

5. Shell Programming and Scripting

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 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... (9 Replies)
Discussion started by: abacus
9 Replies

6. 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

7. Shell Programming and Scripting

extract using sed/awk - need help? Please!!

Need help..not sure how to use with awk or sed I want to take data from the notification.$$ file and assign the data to variable "group". Not sure how to do it. The data I want to extract from the notification.$$ is on the first line of the file ..right after the (notice): NetWorker... (5 Replies)
Discussion started by: gzs553
5 Replies

8. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (1 Reply)
Discussion started by: Shilpi
1 Replies

9. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (2 Replies)
Discussion started by: Shilpi
2 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
Login or Register to Ask a Question