1833871 Members
1767 Online
110063 Solutions
New Discussion

Cut end of lines

 
Kent Friberg_1
New Member

Cut end of lines

Hey,

what is the most efficient way of cutting end of lines in a file when the lines contains filenames for current path ?

Ie.

/path/ll
file.#.1.tmp
file.nbr.2.tmp
file.nr.3.tmp

I want the above to go into a file just without the last 4 chars... The only thing that is certain about the filenames in this folder is that I'm to get rid of the last 4 chars, the filenames before these 4 chars could be anything. The file I want to ends up with should contain the following info:

file.#.1
file.nbr.2
file.nr.3

Appologies for my bad english,,hope this made sense...

thanks
12 REPLIES 12
F. X. de Montgolfier
Valued Contributor

Re: Cut end of lines

Hi,
The answer to your request is cut:

man cut or
http://docs.hp.com/hpux/onlinedocs/B2355-90680/B2355-90680.html
will give you the information on it.

Basically, what you'll have to type is:

cut 4- [file] [>newfile]
where [file] is your file name, and [>newfile] is the optional way of writing the output to newfile.

This will remove the 4 last characters of each line from your file.

Cheers,

FiX
F. X. de Montgolfier
Valued Contributor

Re: Cut end of lines

Ooops,

rewread _then_ post ;-)
Sorry, I did not check my command line before posting...

I, of course, meant for the command to be:

cut -c 4- [file] [>newfile]

Sorry...

FiX
Kent Friberg_1
New Member

Re: Cut end of lines

hmm...i do know a bit of cut...afaik

cut -c 4- [file] [>newfile]

will take this:

file.#.1.tmp
file.nbr.2.tmp
file.nr.3.tmp

and give me this:

.#.1.tmp
.nbr.2.tmp
.nr.3.tmp

thus cutting the *first* 4 chars...not the *last* 4 chars....or am i missing something obvious here ?
H.Merijn Brand (procura
Honored Contributor

Re: Cut end of lines

# perl -pi -e 's/.{4}$//' file_with_files

# reverse < file_with_files | cut -c4- | reverse >xx && mv xx file_with_files

# sed 's/....$//' file_with_file > xx && mv xx file_with_files

There will be zillions of other solutions, including building your own filter with C/Ada/Pascal/Fortran/Cobol/Python/... or evn loading it into a (pseudo)database, changing the text with SQL and extracting it again :))
Enjoy, Have FUN! H.Merijn
F. X. de Montgolfier
Valued Contributor

Re: Cut end of lines

Kent,

both my bad and your bad...
cut -c 4- will cut form the 4th character to the end of line... didn't think before posting.

I can't think of any simple command to do the required cut, now that I think about it...

But the following script should not be far from the result required:

#! /usr/bin/ksh
>[newfile]
for i in `cat [file]`
do
let n=`wc -c $i`
let w=$n -4
cut -c $w- $i >> [newfile]
done

Please remember that the script is untested, as I don't have access to my machine from here...

Sorry about the first two mistakes, I'm _really_ stupid from time to time :-)

Cheers,

FiX
Kent Friberg_1
New Member

Re: Cut end of lines

i was actually about to load it into a sybase sql and do it there with a stored_proc

;)

i suspected that cut wouldnt be able to do this - the sed variant was kinda cool tho so i'll use that one :))

thanks for your help
H.Merijn Brand (procura
Honored Contributor

Re: Cut end of lines

Ohh, and before you dive into scripting, 'basename' might be what you're looking for

# fir i in `ls` ; do
> echo `basename $i` >> file_with_files`
> done
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Cut end of lines

Ohh, and before you dive into scripting, 'basename' might be what you're looking for

# for i in `ls` ; do
> echo `basename $i` >> file_with_files`
> done
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Cut end of lines

cat /path/ll | sed "s/\(.*\)\.\(.*\)$/\1/"


sort of like this:

# echo file.nbr.2.tmp | sed "s/\(.*\)\.\(.*\)$/\1/"
file.nbr.2

or like this:

sed "s/\(.*\)\.\(.*\)$/\1/" /path/ll


Of course if you want the "end piece" you do this:

# echo file.nbr.2.tmp | sed "s/\(.*\)\.\(.*\)$/\2/"
tmp
#


live free or die
harry
Live Free or Die
Leif Halvarsson_2
Honored Contributor

Re: Cut end of lines

Hi,

Use basename for removing extensions.
Example

basename file.nbr.2.tmp .tmp
returns
file.nbr.2

Bill Hassell
Honored Contributor

Re: Cut end of lines

basename is a specialized command just for paths (ie. /a/b/c, etc) and extracts the filename following the last / in the string. However, it is much more efficient to let the shell do this for you:

for MYFILE in $(/usr/bin/ls)
do
print ${MYFILE%.*}
done

So in a directory with files named:

file.#.1.tmp
file.nbr.2.tmp
file.nr.3.tmp
file.with.longer.extension.temp
file.with.just.a.dot.

The result is:

file.#.1
file.nbr.2
file.nr.3
file.with.longer.extension
file.with.just.a.dot

The advantages in using the shell's builtin string handling is speed (no separate process like sed, awk or basename) and unlike cut, this handles any length for the extension, not just 4 characters. It strips everything from the last dot to the end of the filename, including a filename with just a single dot at the end.

The # ## % and %% special characters are often overlooked in string processing scripts.


Bill Hassell, sysadmin
Thomas M. Williams_1
Frequent Advisor

Re: Cut end of lines

Kent:
If you want to create a new file and retain the current file this is all you need:

sed 's/....$//' oldfile > newfile

If you want to change the current file or files:

To remove the last four characters from a number of files use the attached korn shell.

To remove the last four characters from the current file use the following syntax.

vi - yourfile << !
:1,\$s/....$//
:wq!
!

Hope this helps

Thomas


I Think the Clock is Slow ...