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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-24-2007, 02:39 AM
|
#1
|
Member
Registered: Feb 2007
Posts: 65
Rep:
|
Removing columns from a file using `sed`
Hi,
I have a file
aa1:123:sometext1:name1:zz1
bb1:457:sometext2:name2:yy2
cc1:78:sometext3:name3:xx3
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.
indiancosmonaut
|
|
|
09-24-2007, 05:56 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
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.
|
|
|
09-24-2007, 06:14 AM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
awk is the tool to use for working with columns
Code:
awk 'BEGIN{OFS=FS=":"}
{$3="";print }
' "file"
|
|
|
09-24-2007, 06:24 AM
|
#4
|
Member
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519
Rep:
|
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:
Code:
sed 's/^\([^:]*:[^:]*\):[^:]*/\1/' sometext
|
|
|
09-27-2007, 07:14 AM
|
#5
|
Member
Registered: Feb 2007
Posts: 65
Original Poster
Rep:
|
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,
indiancosmonaut
|
|
|
09-27-2007, 08:19 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Another good reference:
http://www.grymoire.com/Unix/Sed.html
indian...
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.
|
|
|
10-04-2007, 01:42 AM
|
#7
|
Member
Registered: Feb 2007
Posts: 65
Original Poster
Rep:
|
Thanks pixellany...
I read some literature and understood some of it.
Will try to learn it in detail... :-)
Kind regards,
indiancosmonaut
|
|
|
All times are GMT -5. The time now is 09:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|