Question about reading and parsing text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about reading and parsing text file
# 1  
Old 07-04-2010
Question about reading and parsing text file

Hello,

I'm just getting started with BASH programming. I would like to write a script to solve a file renaming problem I have. I received a directory containing a collection (>2000) of files whose names are in DOS 8.3 format, and woild like to rename the filenames to a longer and more descriptive text names. The directory also contains a text file that serves as a translation "table of contents" for these files. The text file consists of a list of entries, one per line, listing each file by its filename in 8.3 format followed by the longer descriptive text:
<DOS 8.3 filename 1> <File descriptive name 1>
<DOS 8.3 filename 2> <File descriptive name 2>
<DOS 8.3 filename 3> <File descriptive name 3>
....

My question is: Does anyone have a script that can read the contents of this text file, parse it, and use the parsed fields to rename the other filenames in the directory from the DOS 8.3 name to the descriptive text name?

Many thanks,
Paul

Last edited by polomora; 07-04-2010 at 03:56 PM..
# 2  
Old 07-04-2010
Try:
Code:
awk '{system("mv "$1" "$2)}' filename_toc

# 3  
Old 07-04-2010
If the description contains whitespaces, you'll want to try this:
Code:
while read line; do
mv -v "$(echo $line | cut -d' ' -f1)" "$(echo $line | cut -d' ' -f2-)"
done <file_desc

# 4  
Old 07-04-2010
Quote:
Originally Posted by pseudocoder
If the description contains whitespaces, you'll want to try this:
Code:
while read line; do
mv -v "$(echo $line | cut -d' ' -f1)" "$(echo $line | cut -d' ' -f2-)"
done <file_desc

A much simpler and more efficient way of accomplishing the same thing:
Code:
while read f1 f2; do
    mv "$f1" "$f2"
done < file_desc

Both versions assume that filenames do not contain whitespace or backslashes; if either assumption is invalid, the code will break.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 07-04-2010
Interesting. I did not know that f2 stands for rest of the line...
# 6  
Old 07-04-2010
Actually Alister, since the 8.3 filenames do not contain spaces, f2 - being the last variable in the read statement - always picks up the remainder of the line including whitespace, so in fact your code should work for whitespace in the second part IMO.

S.
# 7  
Old 07-04-2010
Hi, Scrutinizer:

You are absolutely correct, with the exception of trailing whitespace which would be lost. In that case, pseudocoder's `cut -d' ' -f2-` would behave correctly (assuming that the whitespace is part of the name). So the solutions aren't exactly interchangeable.

It goes without saying, though, that filenames with trailing spaces are extremely uncommon (usually the sign of a script or input error), but I considered it for the sake of thoroughness. Personally, I believe that anyone who uses leading/trailing whitespace (or newlines anywhere) in a filename deserves whatever administrative misery befalls them. Smilie

Cheers,
Alister

Last edited by alister; 07-04-2010 at 05:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Text file parsing and comparison

I have two files (first.txt and second.txt): more first.txt cat mammal lizard reptile Elephant mammal ant Insecta more second.txt ant termite ant army_ant (9 Replies)
Discussion started by: cs_novice
9 Replies

2. Shell Programming and Scripting

Parsing text file

Hi Friends, I am back for the second round today - :D My input text file is this way Home friends friendship meter Tools Mirrors Downloads My Data About Us Help My own results BLAT Search Results ACTIONS QUERY SCORE START END QSIZE IDENTITY CHRO STRAND ... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

3. Shell Programming and Scripting

Parsing text file

I'm totally stumped with how to handle this huge text file I'm trying to deal with. I really need some help! Here is what is looks like: ab1ba67c331a3d731396322fad8dd71a3b627f89359827697645c806091c40b9 0.2 812a3c3684310045f1cb3157bf5eebc4379804e98c82b56f3944564e7bf5dab5 0.6 0.6... (3 Replies)
Discussion started by: comp8765
3 Replies

4. Programming

Parsing a Text file using C++

I was trying to parse the text file, which will looks like this ###XYZABC#### ############ int = 4 char = 1 float = 1 . . ############ like this my text file will contains lots of entries and I need to store these entries in the map eg. map.first = int and map.second = 4 same way I... (5 Replies)
Discussion started by: agupta2
5 Replies

5. Shell Programming and Scripting

Need help parsing a text file

I have a text file: router1#sh ip blah blah | incl --- Gi2/8 10.60.4.181 --- 10.60.123.175 11 0000 0000 355K Gi2/8 10.60.83.28 --- 224.10.10.26 11 F9FF 3840 154K Gi2/8 10.60.83.198 --- ... (1 Reply)
Discussion started by: streetfighter2
1 Replies

6. UNIX for Dummies Questions & Answers

Help parsing and replacing text with file name

Hi everyone, I'm having trouble figuring this one out. I have ~100 *.fa files with multiple lines of fasta sequences like this: file1.fa >xyzsequence atcatgcacac...... ataccgagagg..... atataccagag..... >abcsequence atgagatatat..... acacacggd..... atcgaacac.... agttccagat.... The... (2 Replies)
Discussion started by: mycoguy
2 Replies

7. Shell Programming and Scripting

Parsing text from file

Any ideas? 1)loop through text file 2)extract everything between SOL and EOL 3)output files, for example: 123.txt and 124.txt for the file below So far I have: sed -n "/SOL/,/EOL/{p;/EOL/q;}" file Here is an example of my text file. SOL-123.go something goes here something goes... (0 Replies)
Discussion started by: ndnkyd
0 Replies

8. Shell Programming and Scripting

Need help in parsing text file contents

Hi, I need some help in extracting the Exception block between the lines 21 Feb 01:18:54:146 ERROR com.orbits.frameworks.integrationframework.ValidationException - Caught exception in validateRequest() (PID=565584) and 21 Feb 01:18:55:149 INFO ... (0 Replies)
Discussion started by: Alecs
0 Replies

9. Shell Programming and Scripting

Text File Parsing

Hey Guys.I am a newbie on Bash Shell Scripting and Perl.And I have a question about file parsing. I have a log file which contains reports about a communication device.I need to take some of the reports from the log file.Its hard to explain the issue.but shortly I can say that, the reports has a... (2 Replies)
Discussion started by: Djlethal
2 Replies

10. UNIX for Dummies Questions & Answers

Text parsing question

How would I split a file based on the location of a string, basically I want all entries above the string unix in this example 1 2 3 4 unix 5 6 7 Thanks, Chuck (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question