LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Very Basic: replace value flanked by tabs (https://www.linuxquestions.org/questions/linux-newbie-8/very-basic-replace-value-flanked-by-tabs-775648/)

snakefact 12-14-2009 05:29 PM

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-

kbp 12-14-2009 05:36 PM

Give this a try:

Code:

sed -i 's/^\([[:digit:]]\{1,\}\t\)[[:digit:]]\{1,\}\t\(.*\)/\1\2/' /path/to/my/file
cheers

snakefact 12-14-2009 05:47 PM

Quote:

sed -i 's/^\([[:digit::]\{1,\}\t\)[[:digit:]]\{1,\}\t\(.*\)/\1\2/g'
says unterminated command

pixellany 12-14-2009 06:06 PM

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?

kbp 12-14-2009 06:18 PM

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


snakefact 12-14-2009 06:32 PM

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. :)

snakefact 12-14-2009 07:19 PM

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'

Tinkster 12-14-2009 07:55 PM

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

ghostdog74 12-14-2009 08:07 PM

Quote:

Originally Posted by snakefact (Post 3791545)
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 08:48 AM.