awk for a string in a filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk for a string in a filename
# 1  
Old 02-21-2013
awk for a string in a filename

Hi there

I have the variable command below in a script to list the file name in a directory:
Code:
FILEN="$(ls <PATH>  | awk '{print $1}')"

.
When it is run, it correctly returns the following:
Quote:
MYFILE_NAME_20130221000026.CSV
. I would like to be able to cut the date/time portion of the filename and store it as a variable for later use, is it possible to do this, and what is the best method to achieve it?

Thank you
# 2  
Old 02-21-2013
I would not use ls nor awk at all but globbing in the shell.

Code:
$ for f in *.[Cc][Ss][Vv]; do echo "$f"; dt=${f##*_}; dt=${dt%%.*}; echo "$dt"; done
MYFILE_NAME_20130221000026.CSV
20130221000026

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 02-21-2013
This is one way to do it:
Code:
$ FILEN="MYFILE_NAME_20130221000026.CSV"

$ datetime=$(echo $FILEN | tr -cd '[[:digit:]]')

$ echo $datetime
20130221000026

This User Gave Thanks to spacebar For This Post:
# 4  
Old 02-24-2013
Thanks Guys, that has helped a lot!
# 5  
Old 02-24-2013
Bash pattern replacement:
Code:
for file in *.[Cc][Ss][Vv]; do echo ${file//[^0-9]/}; done

This User Gave Thanks to Yoda For This Post:
 
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 grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below. ./test.sh admin_usr.txt Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does. Note: I do not wish to... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Append string to filename

Hi, i wrote the script like... v_date=`date "+%Y%m%d"`; v_date1="rejects_"$v_date'.txt'; echo $v_date1; awk -F\| 'NF==4{ print $0>$v_date1;next} NF!=4 {print $0 >"murali.txt";next}' test1.txt while append data into file as coming like file is $v_date1,but i want data into file... (4 Replies)
Discussion started by: bmk
4 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. Shell Programming and Scripting

remove the filename from a string

I have a string like this /Development/ST/st000001su/Outbound/Prod/PROD-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP/PGP.txt I want to remove the PGP.txt and I want only the /Development/ST/st000001su/Outbound/Prod/MCFR-732QCJ/63acf2caf91bc136cb9bcce8a85c7fa8/PGP returned. I saw an command... (2 Replies)
Discussion started by: srini0603
2 Replies

8. UNIX for Dummies Questions & Answers

Replacing string with filename

Hi All, I've recently run a script that inserts the filename into all files of my active directory. Now I want to move the filename string and have it replace text a few lines down. In other words, here's what I'm trying to do. Here is a file called 'goodtimes': " goodtimes Hi, Welcome... (1 Reply)
Discussion started by: calrog
1 Replies

9. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

10. Programming

filename to string

working with the Win32API I am trying to implement a function in my program that will take the full filename and path of a shortcut (.lnk) file and returns an std::string that is the path+filename+arguments of the program to run, and also a STARTUPINFO structure that can be passed to... (1 Reply)
Discussion started by: cprognew
1 Replies
Login or Register to Ask a Question