|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Thanks a lot, that works perfectly!
|
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > Linux Help > Editing txt files with command line |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|