Help parsing filename with bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help parsing filename with bash script
# 1  
Old 01-12-2011
Help parsing filename with bash script

Hi all!

Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to "livingroom-110111-10.56.37" or maybe "livingroom-2011.Jan.11-10.56.37". Any advice would be great!!

Thanks in advance!
# 2  
Old 01-12-2011
Example with GNU awk on Linux:

Code:
$> echo livingroom-110111105637.avi| awk -F[-.] '{d=substr($2,0,6); h=substr($2,7,2); m=substr($2,9,2);s=substr($2,11,2); print $1"-"d"-"h"."m"."s"."$3}'
livingroom-110111-10.56.37.avi

If "-" is not always the delimeter between name and date/time, you might want to modify this to your needs.

Last edited by zaxxon; 01-12-2011 at 11:09 AM.. Reason: Wrong character positions, corrected.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 01-12-2011
Quote:
Originally Posted by zaxxon
Example with GNU awk on Linux:

Code:
$> echo livingroom-110111105637.avi| awk -F"[-.]" '{d=substr($2,0,6); h=substr($2,6,2); m=substr($2,8,2);s=substr($2,10,2); print $1"-"d"-"h"."m"."s"."$3}'
livingroom-110111-11.05.63.avi

If "-" is not always the delimeter between name and date/time, you might want to modify this to your needs.
Amazing! Thank you very much! Looks like it's off by 1 position though, the 7 at the end is cut off, the output for that example should be "livingroom-110111-10.56.37.avi"
# 4  
Old 01-12-2011
Code:
 echo livingroom-110111105637.avi | sed -r 's@(.*-......)(..)(..)(..)@\1-\2.\3.\4@'

This User Gave Thanks to JavaHater For This Post:
# 5  
Old 01-12-2011
Wow, you guys are quick! Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing filename with sed in bash script

I need to cat two files with similar names. I am using the following script: #!/bin/bash if ] then file=$1 file2="${file%R1.fastq}R2.fastq" echo fetching data from R2 file ... sleep 3 cat $file $file2 > infile else echo "Input_file passed... (2 Replies)
Discussion started by: Xterra
2 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Parsing and Editing a json file with bash script

I am trying to automate editing of a json file using bash script. The file I initially receive is { "appMap": { "URL1": { "name": "a" }, "URL2": { "name": "b" }, "URL3": { "name": "c" }, } WHat I would like to do is replace... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

4. Shell Programming and Scripting

Bash script not parsing file with spaces in path

Hi everyone, I'm trying to write my first ever shell script, the OS is Raspbian. The code I have written must be executed whenever a certain database has been modified. The database resides on a Windows server to which I have a mount point, and I have no control over the Windows server at all so... (2 Replies)
Discussion started by: gjws
2 Replies

5. Shell Programming and Scripting

How to Pass filename to AWK in bash script

I have written a script which works fine, to remove patterns contained in EXCLUDE.DAT from input.txt awk 'BEGIN {n=0;while (getline < "EXCLUDE.DAT" > 0){ex=$0;n++}} {for(var in ex){print var "-" ex $0 ;i++}}' input.txt The last problem I need to solve is how to pass the file... (3 Replies)
Discussion started by: nixie
3 Replies

6. Shell Programming and Scripting

Column parsing in a bash script - HELP

I would like to setup a script that pulls in time/date in two seperate columns, and then name the other columns as listed below: Column1=Art/TJ output Column2=Art/TJ output Column3=TJ output column4=Art output Column5=If time/date past 12:00 noon -fail Colume6=If time/date before... (1 Reply)
Discussion started by: walnutpony123
1 Replies

7. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

8. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

9. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

10. Shell Programming and Scripting

Conversion of bash parsing script to perl?

I need help with a perl parsing script. I have some error logs on a windows machine that I need to parse from a text file, but I know nothing about perl. I usually run this bash script on my linux box and it does just what I need. How would I do the same thing with perl and port it to my windows... (2 Replies)
Discussion started by: cstovall
2 Replies
Login or Register to Ask a Question