Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
|
|
12-14-2009, 06:29 PM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Rep:
|
Very Basic: replace value flanked by tabs
I should know how to do this, but I'm very new to unix.
I have a table of data, which is tab delimited. I need something, preferably sed, that will delete the 2nd value here, while preserving the placeholder:
3 1 1.0 0.10 0.1 1.0e-1 1.0010
to this
3 1.0 0.10 0.1 1.0e-1 1.0010
Thanks for helping a newb-
Last edited by snakefact; 12-14-2009 at 06:32 PM.
|
|
|
12-14-2009, 06:36 PM
|
#2
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
Give this a try:
Code:
sed -i 's/^\([[:digit:]]\{1,\}\t\)[[:digit:]]\{1,\}\t\(.*\)/\1\2/' /path/to/my/file
cheers
Last edited by kbp; 12-14-2009 at 07:17 PM.
Reason: Sorry... typo
|
|
|
12-14-2009, 06:47 PM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Quote:
sed -i 's/^\([[:digit::]\{1,\}\t\)[[:digit:]]\{1,\}\t\(.*\)/\1\2/g'
|
says unterminated command
|
|
|
12-14-2009, 07:06 PM
|
#4
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
Code:
sed 's/\t[^\t]*\t/\t \t/1' filename > newfilename
Simple, but not very flexible---e.g it does not adapt well to changing any field.
Why not AWK? Simply print the fields you want, and print your placeholder (I used a space) for the ones you don't want.
PS: Have you read the material suggested in your earlier thread?
Last edited by pixellany; 12-14-2009 at 07:07 PM.
|
|
|
12-14-2009, 07:18 PM
|
#5
|
Senior Member
Registered: Aug 2009
Posts: 3,790
|
Edited my post above due to a typo, sorry.. didn't test it
And again! geez... you should always test !
Here's a working one:
Code:
sed 's/^\([[:digit:]]\{1,\}[[:blank:]]\)[[:digit:]]\{1,\}[[:blank:]]\(.*\)/\1\2/' ./myfile
Output:
Code:
sed 's/^\([[:digit:]]\{1,\}[[:blank:]]\)[[:digit:]]\{1,\}[[:blank:]]\(.*\)/\1\2/' ./test
3 1.0 0.10 0.1 1.0e-1 1.0010
Last edited by kbp; 12-14-2009 at 07:34 PM.
|
|
|
12-14-2009, 07:32 PM
|
#6
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
For some reason was unable to get either of the above suggestions to work.
However, this works except on values at the beginning or end of the line:
sed -r 's/(\t)1(\t)/\1\2/g'
I tried this to replace the ones the beginning, but it didn't work:
sed -r 's/(\t|^)1(\t)/\1\2/g'
Does anyone have a solution to this problem??
The reason why I'd prefer sed is because I know the syntax a little, whereas with awk, I don't know it at all. But if there's an easy awk solution, then by all means please share.
Last edited by snakefact; 12-14-2009 at 07:40 PM.
|
|
|
12-14-2009, 08:19 PM
|
#7
|
LQ Newbie
Registered: Dec 2009
Posts: 11
Original Poster
Rep:
|
Ended up using this:
sed -r 's/(\t)1(\t)/\1\2/g' | sed -r 's/^1(\t)/\1/g' | sed -r 's/(\t)1$/\1/g'
|
|
|
12-14-2009, 08:55 PM
|
#8
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Note: I've actually inserted TABs where you had spaces.
Code:
$ cat tabs
3 1 1.0 0.10 0.1 1.0e-1 1.0010
awk 'BEGIN{FS=OFS="\t"}{$2=""; print }' tabs
3 1.0 0.10 0.1 1.0e-1 1.0010
Cheers,
Tink
|
|
|
12-14-2009, 09:07 PM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by snakefact
The reason why I'd prefer sed is because I know the syntax a little, whereas with awk, I don't know it at all. But if there's an easy awk solution, then by all means please share.
|
you have seen the simpler and more readable awk solution, now start your learning journey. See my sig for gawk manual
|
|
|
All times are GMT -5. The time now is 11:36 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
|
|