Need help in reading a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in reading a filename
# 1  
Old 03-05-2012
Need help in reading a filename

Hi Guys,

I have the following input.

Code:
test_junk_file__20120210092009.txt
latest_file__20120210092009.txt
side_load_junk_file__20120210092009.txt

I need to exclude the timestamp part from the file.

so, the output should be as follows.

Code:
test_junk_file.txt
latest_file.txt
side_load_junk_file.txt

Could you please help me out here..

Cheers!!!!!!
# 2  
Old 03-05-2012
@mac4rfree

With Sed..
Code:
sed 's/__[0-9]*//' inputfile

# 3  
Old 03-05-2012
Code:
$ cat efs
#!/bin/ksh

file1="test_junk_file__20120210092009.txt"

print "${file1%__*}.${file1#*.}"

exit 0
$ efs
test_junk_file.txt
$

# 4  
Old 03-06-2012
Yeah Guys,,

That Worked perfectly Smilie
# 5  
Old 03-06-2012
oldname=ytyytu_uyhsudh_1218788_1212__212121212.txt
Code:
newname=`echo $oldname|awk -F'__' '{print $1}'`.`echo $oldname|cut -f2 -d '.'`

---------- Post updated at 06:54 AM ---------- Previous update was at 06:53 AM ----------

oldname=ytyytu_uyhsudh_1218788_1212__212121212.txt
Code:
newname=`echo $oldname|awk -F'__' '{print $1}'`.`echo $oldname|cut -f2 -d '.'`


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 03-06-2012 at 08:39 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 03-06-2012
P.S. Here's a resource that includes string operations if your shell supports them:
Reference Cards
# 7  
Old 03-06-2012
on top of elixir
Code:
newname=`echo $oldname|awk -F'_[0-9]' '{print $1}'`.`echo $oldname|cut -f2 -d '.'`

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 03-06-2012 at 10:57 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

reading filename in nested directories

Hi all, I have some directory structure as $ls -ltr drwxr-xr-x 1 mscprod us_msc 512 May 10 08:34 650 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 652 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 640 drwxr-xr-x 1 mscprod us_msc512 May 10 09:13 654 $ cd 650/ $ls fileabc.root and within... (7 Replies)
Discussion started by: emily
7 Replies

3. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. Homework & Coursework Questions

Matlab help! Reading in a file with a variable filename

1. The problem statement, all variables and given/known data: I want to read in a file, and plot the data in matlab. However, I do not like hardwiring filenames into my codes, so I always give the user the option to specify what the filename is. I am pretty inexperienced with matlab, so I have no... (0 Replies)
Discussion started by: ds7202
0 Replies

5. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

6. UNIX for Dummies Questions & Answers

assingn a variable a filename and then reading it in

Im trying to set a filename to a variable and then read the file in using the variable but im getting a syntax error. any ideas? #!/bin/bash function scanFile() { while read $1 do echo $filename done } file1=report.log scanFile() $file1 (3 Replies)
Discussion started by: magnia
3 Replies

7. UNIX for Dummies Questions & Answers

Reading filename from directory

I am using the following code to read filename from the directory: for i in ` ls $inputDir | grep $partialName*.csv` do echo $i done But the echo is giving me the following: ls | grep cm_ctx*.csv instead of the full filename "cm_ctx_2009_07_15_17_18.csv" Any ideas anyone? I... (2 Replies)
Discussion started by: khanvader
2 Replies

8. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 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. Shell Programming and Scripting

Reading a file using sh with spaces in filename

Hi I am trouble parsing through a file with spaces in the filename. I need to grab "supportIDPS/SCM/windows_install/file groups/dds.fgl" and then do a md5sum on it. I am using sh. Any help is appreciated. Here is an example of the input file: 7eedbc9f7902bf4c1878d9e571addf9a ... (4 Replies)
Discussion started by: jekl5
4 Replies
Login or Register to Ask a Question