- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Cut end of lines
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 09:24 AM
12-16-2002 09:24 AM
Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 09:38 AM
12-16-2002 09:38 AM
Re: Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 09:40 AM
12-16-2002 09:40 AM
Re: Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:01 AM
12-16-2002 10:01 AM
Re: Cut end of lines
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:12 AM
12-16-2002 10:12 AM
Re: Cut end of lines
# 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 :))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:14 AM
12-16-2002 10:14 AM
Re: Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:19 AM
12-16-2002 10:19 AM
Re: Cut end of lines
;)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:21 AM
12-16-2002 10:21 AM
Re: Cut end of lines
# fir i in `ls` ; do
> echo `basename $i` >> file_with_files`
> done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 10:21 AM
12-16-2002 10:21 AM
Re: Cut end of lines
# for i in `ls` ; do
> echo `basename $i` >> file_with_files`
> done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 11:25 AM
12-16-2002 11:25 AM
Re: Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2002 12:01 PM
12-16-2002 12:01 PM
Re: Cut end of lines
Use basename for removing extensions.
Example
basename file.nbr.2.tmp .tmp
returns
file.nbr.2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2002 05:15 AM
12-17-2002 05:15 AM
Re: Cut end of lines
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2002 05:56 AM
12-17-2002 05:56 AM
Re: Cut end of lines
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