Visit Jeremy's Blog.
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 09-24-2007, 02:39 AM   #1
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
Removing columns from a file using `sed`


I have a file


Could you help me with a `sed` command to remove entire column-3 i.e. the sometext column.

PS: Share with me some good literature for sed.

Kind regards and thanks in advance.

Old 09-24-2007, 05:56 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390

There's an easier way to cut out this column: The cut command.

cut -d: -f1,2,4,5 infile

If you do want/need a sed solution:

sed 's/\(.*:.*\):.*:\(.*:.*\)/\1:\2/' infile

A good reference for Sed (and AWK): O'Reilly's 'sed & awk'
Or on-line: the SED $HOME

Hope this helps.
Old 09-24-2007, 06:14 AM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk is the tool to use for working with columns
awk 'BEGIN{OFS=FS=":"}
     {$3="";print }
' "file"
Old 09-24-2007, 06:24 AM   #4
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Indiancosmonaut,

I can't share any literature with you as I just read "man" and "info" for the trivial tasks and, for the rest, I google.

The following code uses regexp to get 3 groups of non-colon characters separated with a colon, from the start of the line; the first 2 can be referenced after by enclosing them between parenthesis; they will be referenced as variable 1 on the second part of the sed expression:
sed 's/^\([^:]*:[^:]*\):[^:]*/\1/' sometext
Old 09-27-2007, 07:14 AM   #5
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Hi Druuna and ghostdog,

Thanks for the solutions and the Link. Its got lots of information!!!
I'll try to find the good ones...

Hi osvaldomarques,

Thanks for the solution you gave but I'm unable to fully understand it.
It goes over my head! I'll try to read more about this. Meanwhile, could you kindly explain it in a little detail so that I am able to understand it.

Kind regards,

Old 09-27-2007, 08:19 AM   #6
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Another good reference:


osvaldo gave you a pretty complete explanation--what makes it hard is that the "backreference" is embedded in other code.

First, look at the simple syntax for a backreference. This code finds the first occurrence of "yp" and replaces it with "y":
sed 's/\(y\)p/\1/' filename
In a backreference, everything in the escaped parentheses is inserted where the "\1" occurs. Obviously, this could be done with:
sed 's/yp/y/' filename

The best way to learn sed is to write very simple commands first--once you know how they work, then add more layers of complexity.
Old 10-04-2007, 01:42 AM   #7
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks pixellany...

I read some literature and understood some of it.
Will try to learn it in detail... :-)

Kind regards,



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
how to append columns form a column file in another file adam_blackice Programming 4 09-18-2007 12:33 AM
help removing some text from a file (sed) BrianK Programming 2 08-13-2006 08:10 PM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 08:20 AM
Removing the "enter" symbols in the columns of emacs? Erik_the_Red Linux - Software 2 07-18-2005 05:03 PM
Removing Columns From Output darthtux Programming 4 08-01-2004 10:21 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 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