Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-02-2008, 07:09 AM   #1
C's Brother
LQ Newbie
Registered: Aug 2008
Posts: 5

Rep: Reputation: 0
chopping away unwanted parts in string

Hi masters,

I am learning some bash programming; and it's not going too well, I'm afraid... :-(
I try to get the hang of "chopping off" unwanted information.

The problem is this output of the du command:
1. I want to get rid of the "./" at the beginning of ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch13
2. How to get rid of the /export/home part of the total string.
3. How to get rid of the total sum the "du" command leaves behind at the end of the first column.

72 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch13
72 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch4
166 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch14
94 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch9
213 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch3
126 ./export/home/john/Documents/BOOKS/DNS/books/ldap/apd
72 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch7
72 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch10
1568 ./export/home/john/Documents/BOOKS/DNS/books/ldap/apc
706 ./export/home/john/Documents/BOOKS/DNS/books/ldap/apa
151 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch2
72 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch15
155 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch8
723 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch5
94 ./export/home/john/Documents/BOOKS/DNS/books/ldap/ch12

Thanks alot!

Cheers, C's brother
Old 11-02-2008, 07:22 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737Reputation: 737
What have you tried so far?

The examples you give can be easily done with SED. For example, to delete a specific phrase---eg "/export/home", you would do this:

sed 's%/export/home%%' <filename >newfilename

This uses the SED substitution command, with "%" as the delimiter. The general syntax is "s%old%new%". In this case, I chose "%" as the delimiter so there would not be a conflict with the "/" in the actual test. ("/" is the delimiter seen most often in the SED documentation.)

To remove the last line, you can use SED, CUT, and probably several others.

Best SED tutorial here:

Bash tutorials:
Old 11-02-2008, 07:34 AM   #3
C's Brother
LQ Newbie
Registered: Aug 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Originally Posted by pixellany View Post
What have you tried so far?

regexp in Vi, but I was looking for something automated.
Thanks for the SED hint.
Works great!

Old 11-02-2008, 07:56 AM   #4
Sergei Steshenko
Senior Member
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Look up "perl one-liner".


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
Chopping and encoding a portion of a DVD video trashbat Linux - Software 2 09-17-2008 05:03 PM
chopping up directories with sed: trscookie Programming 8 02-29-2008 03:22 AM
getting a sedache when chopping the end off a line: trscookie Programming 2 01-31-2008 05:12 AM
Sound chopping when switching windows Lord_Grave Linux - Hardware 2 01-18-2006 06:43 PM
complete string conversion - without parts evaluation slackie1000 Programming 9 01-06-2004 01:10 PM

All times are GMT -5. The time now is 11:26 PM.

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