The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Trying to remove '^M' characters from a file. Krispy Shell Programming and Scripting 13 05-12-2009 05:46 PM
sed to remove last 2 characters of txt file ajp7701 Shell Programming and Scripting 1 10-29-2008 07:23 PM
remove space characters melanie_pfefer Shell Programming and Scripting 1 03-11-2008 01:45 PM
How to remove Characters before '~' Amey Joshi UNIX for Dummies Questions & Answers 4 01-07-2008 06:43 AM
Remove control characters aravind_mg UNIX for Dummies Questions & Answers 5 10-02-2002 01:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-28-2008
chriss_58 chriss_58 is offline
Registered User
  
 

Join Date: May 2008
Posts: 41
Remove lasts characters from a string

Hi all,

Consider i have a directory /tmp/test and inside this directory i have the following files:
1.svf.tmp
2.svf.tmp
3.svf.tmp

How can i remove the last four characters of every file in irder for the directory to be as:
1.svf
2.svf
3.svf

I use the following command but id doesn't seem to work
sed 's/\(.*\).../\1/'

Thamk you!
  #2 (permalink)  
Old 11-28-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
Code:
cut -d. -f1,2 infile
  #3 (permalink)  
Old 11-28-2008
dddkiran dddkiran is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 24
ls -1 | awk '{print substr($0,1,length($0)-4)}'
  #4 (permalink)  
Old 11-28-2008
chriss_58 chriss_58 is offline
Registered User
  
 

Join Date: May 2008
Posts: 41
Sorry but nothing of the above seems to work.

have in mind that the files inside /tmp/test directory could be more than 1000. So i need to create a script that will remove the '.tmp' from all the 1000 files
  #5 (permalink)  
Old 11-28-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

Code:
for i in *tmp; do mv $i ${i%.tmp}; done
This uses shell's build in string manipulations feature to remove the last four characters .tmp.

HTH Chris
  #6 (permalink)  
Old 11-28-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
cut has no problems with more than 1000 lines; nor has awk or sed.

Code:
root@isau02:/data/tmp/testfeld> cat infile
1.svf.tmp
2.svf.tmp
3.svf.tmp
root@isau02:/data/tmp/testfeld> cut -d. -f1,2 infile
1.svf
2.svf
3.svf
According to the directory listing you can just do
Code:
ls -1 /tmp/test | cut -d. -f1,2 infile
Maybe you tell us what kind of error you get if it still doesn't work.
  #7 (permalink)  
Old 11-28-2008
chriss_58 chriss_58 is offline
Registered User
  
 

Join Date: May 2008
Posts: 41
It worked..
Thank you very much all..
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0