LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-14-2009, 05:29 PM   #1
snakefact
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
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 05:32 PM.
 
Old 12-14-2009, 05:36 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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 06:17 PM. Reason: Sorry... typo
 
Old 12-14-2009, 05:47 PM   #3
snakefact
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
sed -i 's/^\([[:digit::]\{1,\}\t\)[[:digit:]]\{1,\}\t\(.*\)/\1\2/g'
says unterminated command
 
Old 12-14-2009, 06:06 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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 06:07 PM.
 
Old 12-14-2009, 06:18 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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 06:34 PM.
 
Old 12-14-2009, 06:32 PM   #6
snakefact
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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 06:40 PM.
 
Old 12-14-2009, 07:19 PM   #7
snakefact
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Original Poster
Rep: Reputation: 0
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'
 
Old 12-14-2009, 07:55 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 12-14-2009, 08:07 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by snakefact View Post
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
 
  


Reply



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
GnuCash, KMyMoney - basic, basic questions - please help if you can. brjoon1021 Linux - Software 3 10-20-2008 06:27 PM
Need a basic accounting / invoicing software to replace Windows/Quicken/Quickbooks. brjoon1021 Linux - Software 8 10-15-2008 03:53 PM
b.it.c.h.x tabs Smokey Linux - Software 0 11-09-2004 01:52 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
I'm a BASIC chap, looking for some info on BASIC programming CragStar Programming 2 01-21-2001 09:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:41 AM.

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