LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2005, 08:17 AM   #1
linuxchump
LQ Newbie
 
Registered: Mar 2005
Posts: 10

Rep: Reputation: 0
Remove last character from file/string


Hi!

I've got a file that is comma delimited, such as;


134,145,245,678,5888,10000,24323,


The problem I'm facing is I want to remove just the last comma such that the file retains all number and commas minus the last one. I am brain farting after GOOGLING all night .... I saw much reference to 'chomp' and 'chop' using PERL, but I'm trying to keep this very simple.

Is there a command in Linux that can be used or included in a simple script to remove this last character (the comma) from the file?

Any information or pointers are greatly appreciated!!!

-lc
 
Old 07-21-2005, 08:21 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,541

Rep: Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866
try :
Code:
sed -ie 's/,$//' file
 
Old 07-21-2005, 08:22 AM   #3
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
If comma is always the last character, you can try something like
Code:
rev filename | cut -b 2- | rev
Basically, it reverses each line, cuts from the 2nd character to the end of line and reversing it back.

HTH
 
Old 07-21-2005, 09:09 AM   #4
linuxchump
LQ Newbie
 
Registered: Mar 2005
Posts: 10

Original Poster
Rep: Reputation: 0
resolved!

Thank you both kindly. Each worked. Very happy. Now going to sleep!

-lc
 
Old 07-24-2005, 05:53 PM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
The sed solution is preferable, since it's a single process, which is quicker to run than 3(!), and is more obvious (for maintenance purposes).
 
Old 04-08-2009, 02:47 AM   #6
hi_irf
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Remove specific characters from text file

Hi,

I have following lines in my text file. File contains more than 20 million lines. like

/home/apache/ht/images/5/1/200904021159351.jpg
/home/apache/ht/images/5/1/20090401113951.jpg
/home/apache/ht/images/5/1/200904041728451.jpg
/home/apache/ht/images/9/5/2561195/2561195-20090402015241.xls
/home/apache/ht/images/attachments/9/5/2561195/2561195-20090407011002.xls
/home/apache/ht/images/attachments/9/5/2561195/2561195-20090406090748.xls


Now i want to remove filename from entire file. File should look like this


/home/apache/ht/images/5/1/
/home/apache/ht/images/5/1/
/home/apache/ht/images/5/1/
/home/apache/ht/images/9/5/2561195/
/home/apache/ht/images/attachments/9/5/2561195/
/home/apache/ht/images/attachments/9/5/2561195/


can any one let me know the command please. It would be highly appreciated.
 
Old 04-08-2009, 02:59 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you should start a new thread. anyway, you can use this
Code:
# awk -F"/" '{$NF=""}1' OFS="/" file
/home/apache/ht/images/5/1/
/home/apache/ht/images/5/1/
/home/apache/ht/images/5/1/
/home/apache/ht/images/9/5/2561195/
/home/apache/ht/images/attachments/9/5/2561195/
/home/apache/ht/images/attachments/9/5/2561195/
 
Old 04-09-2009, 01:45 AM   #8
hi_irf
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Remove specific characters from text file

Hi ghost,

It works for me. Thanks for your kind help. Can you explain that syntax for me please.

awk -F"/" '{$NF=""}1' OFS="/" file

what is $NF and OFS ?


Another question i am running find command on the directory. I am getting list of files created or modified in last 24 hours through following command.

find testdir -mtime -1 -type f -print >> images.log

where testdir have 20 subdirectories and each subdirectory have more than 1 directories beneath. So my command took lot of time consuming resources extensively because it has to search in every directory and beneath. I have two questions in mind.

1) What should be the command to search for files created or modified in last 12 hours.
2) What Should be the less expensive command to search means can i search through timestamp for files in last 24 hours ? Like

find . -name -mtime mm hh date mm:ss type -f > test.txt


Thanks in advance.
 
Old 04-09-2009, 02:28 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by hi_irf View Post
Hi ghost,

It works for me. Thanks for your kind help. Can you explain that syntax for me please.

awk -F"/" '{$NF=""}1' OFS="/" file

what is $NF and OFS ?
-F"/" -> set field separator as "/"
NF -> number of field
$NF -> value of the last field
OFS -> output field separator.

all these are explained in the man page or awk. or you can take a look at effective awk.
 
Old 04-09-2009, 02:49 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another solution using sed
Code:
sed 's/\(.*\/\).*$/\1/g' file
 
Old 04-10-2009, 03:08 AM   #11
hi_irf
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Hi,

can you explain the command please

find . -name -mtime mm hh date mm:ss type -f

Let suppose how could i find today(Apr 10) files through this command ?
 
Old 04-10-2009, 03:46 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by hi_irf View Post
Hi,

can you explain the command please

find . -name -mtime mm hh date mm:ss type -f

Let suppose how could i find today(Apr 10) files through this command ?
The sed command was just another solution to the previous issue. To find files more recent than a specific date, you can create a dummy file using the touch command with the -t option, e.g:
Code:
touch -t200904101200 /tmp/dummy_file
then use the test -newer in the find command:
Code:
find . -type f -newer /tmp/dummy_file
 
Old 04-10-2009, 05:15 AM   #13
hi_irf
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
Hi,

My question is find should not work on entire directory as it will take lot of time and increases I/O. Is there any command that directly work on month. Like find search files which are created or modified in month April
directly and escape other files without looking into them.
 
Old 04-10-2009, 05:22 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Nope. The find command must look at every single item under the search path to see if it matches the search criteria. If you have billions of files under the search path, it will look at them all. No chance to skip some check in my opinion.
 
Old 04-10-2009, 12:39 PM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,308

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would basename work
man basename

then maybe you could sed s/`the return of basename`/""/ in a loop ?

Last edited by schneidz; 04-10-2009 at 12:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
Picking a character from a string randomly swatward Programming 2 08-14-2005 01:21 AM
how to print the first character in a string using strtok its_godzilla Programming 5 02-02-2005 10:22 AM
how to solve Character and String problems on linux?(screenshot) tradingbr Linux - General 0 04-29-2004 02:51 PM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration