Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 28th, 2003, 01:50 PM
sam_rich sam_rich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Texas
Posts: 82 sam_rich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 40 m 34 sec
Reputation Power: 6
Editing txt files with command line

Let say you have a text file with the following list:


./File1.doc
./File2.doc
./Folder1/File3.doc
./Folder2/File4.doc


How can you remove the ./ from the list using the command line? Or some type of script that can be called from the command line? After executing the command the results could be piped to a new file if that helps. Results of the new file would be:

File1.doc
File2.doc
Folder1/File3.doc
Folder2/File4.doc

Thanks.

Reply With Quote
  #2  
Old March 28th, 2003, 02:38 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 42 m 50 sec
Reputation Power: 184
cut -c 3- oldfile.txt > newfile.txt

"cut -c 3-" says "select third character of each line till the end of it."
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old March 28th, 2003, 03:03 PM
sam_rich sam_rich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Texas
Posts: 82 sam_rich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 40 m 34 sec
Reputation Power: 6
Thanks a lot, that works perfectly!

Reply With Quote
  #4  
Old March 28th, 2003, 07:42 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 38 sec
Reputation Power: 480
In the spirit of "there's more than one way" and for a more general solution that will only change the lines starting with ./ and leave all others alone, you could try sed:
Code:
sed 's/^\.\///' oldfile.txt   > newfile.txt


In case you're not familiar with it, the substitute command's syntax is:
Code:
s/regular expression/substitution string/[options]

The regular expression I gave it was to match a ./ at the beginning of the line(^); because . and / also have special meanings here, they had to be quoted with a back-slash. The substitution string was empty, which just deleted the matched substring. Options could include:
g -- global; substitute all matches within a line, not just the first one found
i -- case-insensitive

Just for fun, I also gave the old line editor, ed, a try. If you open a file with ed and redirect a script file of commands as input, then you can edit the file and save it under its original name:
Code:
ed oldfile.txt <ed-script  

ed-script
1,$s/^\.\///
w
q


The commands of ed have been incorporated in vi's ex editor, which you access with a colon. As in the ex editor, the "1,$" is an address range of lines 1 through to the last line, "w" means write to the file, and "q" means quit.

The only problem with the ed example is that ed-script had to have been prepared before-hand.

Probably could also work a one-liner in awk or perl, but it's getting late.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Editing txt files with command line


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT