how to get a particular word from a file name of different format.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get a particular word from a file name of different format.
# 1  
Old 01-21-2009
how to get a particular word from a file name of different format.

Hi all,

I need help in getting a particular string from the file name.let me describe my need, i have a directory that has different kind of files

for Eg:

fileone_rat_21012009.txt
filetwo_new_file_21012009.txt
file_old_ntn4_21012009.txt

i have different file formats but all the files end with "_21012009.txt" , here i need to get only the date ie " "21012009" from any one of the file present at the moment in the diretory.

please help me. Thanks in advance.
# 2  
Old 01-21-2009
Code:
ls | egrep -o "21012009"

That would get only the date.

However, if you are wanting to get the date portion from the file name, for any possible date, then you could do..

Code:
ls | egrep -o "[0-3]?[0-9][01]?[0-9]2[0-9]{3}"

Or an easier way without regex.

Code:
ls | awk -F_ '{print $(NF) | "cut -d. -f1"}'

Of course you will need to weed out any other files that may be in there..
# 3  
Old 01-21-2009
Quote:
Originally Posted by ulin
I need help in getting a particular string from the file name.let me describe my need, i have a directory that has different kind of files

for Eg:

fileone_rat_21012009.txt
filetwo_new_file_21012009.txt
file_old_ntn4_21012009.txt

i have different file formats but all the files end with "_21012009.txt" , here i need to get only the date ie " "21012009" from any one of the file present at the moment in the diretory.

There's no need for an external command:

Code:
set -- *
temp=${1##*_}
date=${temp%.txt}

printf "%s\n" "$date"

# 4  
Old 01-21-2009
Wow, I am um lost now..

Could you please explain the variable assignment? (and the set -- * ). Thanks!
# 5  
Old 01-21-2009

Code:
## Put all filenames into the positional parameters
set -- *

## Get the portion of the first filename following the last underscore
temp=${1##*_}

## Remove .txt 
date=${temp%.txt}

## Print the result
printf "%s\n" "$date"

For more information, read your shell's man page.
# 6  
Old 01-21-2009
Thanks you both !!!!, it worked .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Programming

Arrange word in table metrix format

Hello everyone, I have some problem about this code : #!/usr/bin/env python import sys try : filename = sys.argv except : print 'Specify filename' sys.exit() fd = open(filename) lines = fd.xreadlines() compare = {} for line in lines : split_line =... (1 Reply)
Discussion started by: awil
1 Replies

4. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

5. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

6. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

7. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

8. UNIX for Dummies Questions & Answers

Convert UTF8 Format file to ANSI format

:confused: Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on... (9 Replies)
Discussion started by: rajreddy
9 Replies

9. UNIX for Advanced & Expert Users

Convert UTF8 Format file to ANSI format

:) Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on this.........Let me... (1 Reply)
Discussion started by: rajreddy
1 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question