Taking letter from a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking letter from a word
# 1  
Old 03-30-2009
Power Taking letter from a word

Hi All,

I am new to UNIX and i am trying to write a script.My requirement is that from the following logs i need to get the following outputs:
abc_lifecycle.log
bcde_enjoy.log
abc_twinkle.log

Output expecting:

lifecycle
enjoy
twinkle

Could you please help me in getting this?
# 2  
Old 03-30-2009
Code:
echo 'abc_lifecycle.log
  bcde_enjoy.log
  abc_twinkle.log' | awk -F [_.] '{ print $2 }'

# 3  
Old 03-30-2009
See "man basename".

Code:
basename abc_lifecycle.log .log|awk -F_ '{print $2}'
lifecycle


Last edited by methyl; 03-30-2009 at 09:16 PM.. Reason: wrt cfajohnson comment and OP requirement
# 4  
Old 03-30-2009
Quote:
Originally Posted by methyl
See "man basename".

Code:
basename abc_lifecycle.log .log
abc_lifecycle


Apart from the fact that that is not what the OP asked for, the basename command is entirely unnecessary in a POSIX shell (which is available on every system made in the last 10 to 15 years at least) and on any system before that which had ksh.

Code:
string=abc_lifecycle.log
temp=${string##*_}
printf "%s\n" "${temp%.*}"

If there are many names to be processed, then the awk script I posted previously may be quicker.
# 5  
Old 03-30-2009
Bug Thanks

Thanks Johnson.

But the problem is that i have just given only 3 log samples.In general i have 74 logs like this separated by such underscores.I need to get that 74 log names alone.
# 6  
Old 03-30-2009
Code:
echo abc_lifecycle.log | sed 's/\..*$//'

or

Code:
var=abc_lifecycle.log
echo ${var/%.*/}

# 7  
Old 03-31-2009
Quote:
Originally Posted by summer_cherry
Code:
echo abc_lifecycle.log | sed 's/\..*$//'

or

Code:
var=abc_lifecycle.log
echo ${var/%.*/}


Code:
$ echo ${var/%.*/}
Syntax error: Bad substitution

When using non-standard syntax, you should specify where it does work. That line will work in bash and ksh93.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

3. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

4. Shell Programming and Scripting

SED (or other) upper to lowercase, with first letter of first word in each sentence uppercase

The title pretty much defines the problem. I have text files that are all in caps. I would like to convert them to lowercase, but have the first letter of the first word in each sentence in uppercase. I already have SED on the server for fixing / tweaking text files, but I'm open to other... (5 Replies)
Discussion started by: dockline
5 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

Convert to upper case first letter of each word in column 2

Hi guys, I have a file separated by ",". I´m trying to change to upper case the first letter of each word in column 2 to establish a standard format on this column. I hope somebody could help me to complete the SED or AWK script below. The file looks like this: (Some lines in column 2... (16 Replies)
Discussion started by: cgkmal
16 Replies

7. Shell Programming and Scripting

Trying to capitalize first letter of every word in Variable

Total Bash noob, have been successful in doing my script by searching and looking at examples, but I need some assitance with this one, just can't figure it out. In the Bash script I am trying to capitalize the first letter of every word in a string, ideally not changing other capitalization. ... (5 Replies)
Discussion started by: randyharris
5 Replies

8. Shell Programming and Scripting

matching a letter in a word

hi, if i have a string of letters and seperatly i have a single letter. how do i check whether that specific letter is in my string aswell? any ideas? (2 Replies)
Discussion started by: Furqan_79
2 Replies

9. Shell Programming and Scripting

First letter of each Word from a line

Hello All, I am new to UNIX, how do i get the First letter of each Word from a line in shell scripting. For Example line = "The Jack In The Box" I want to reterive The letters T for The, J from Jack, I from In, T from The and B from Box. and store in another string. Can anyone... (5 Replies)
Discussion started by: maxmave
5 Replies

10. UNIX for Advanced & Expert Users

How to filter the words, if that word contains the expected letter

Hi, I am trying to filter the words from a file which contain 'abc'. But I am unable to. Could any one help me. For eg: The file contents are 123ab 12hnj1 123abc456 123cgbcahjkf23 23134abchfhj43 gc32abc abc1 2abc3 sd uiguif fhwe 21242 uh123 jkcas124d123 u3hdbh23u ffsd8 Output... (3 Replies)
Discussion started by: venu_eie
3 Replies
Login or Register to Ask a Question