hot to specify the last column in sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting hot to specify the last column in sed?
# 1  
Old 06-20-2008
hot to specify the last column in sed?

should be a pretty simple problem, but it appears that I can't not specify the last column using sed

say I want to replace every aa with YY except the last colum in file test

$ cat /tmp/test
aa aa aa aa aa
c b c d aa a
e f ff mm aaa aaa 123

I was thinking about using

sed '/ [^ ]*$/!s/aa/YY/g' /tmp/test

but I just realized that this is totally wrong, so how can I specify the last column in sed?

Thanks!

Last edited by fedora; 06-20-2008 at 04:31 PM..
# 2  
Old 06-20-2008
I know this is not sed, but with awk I think is a lot simpler:

Code:
awk '{ for (i=1;i<NF;i++) if ($i=="aa") $i="YY"; print }' input_file.txt

# 3  
Old 06-20-2008
thanks for the answer, but can I just do it using sed?
# 4  
Old 06-20-2008
With GNU sed you may try something like this:

Code:
sed "s/\baa\b/YY/g; s/YY$/aa/" input_file.txt

No better ideas... For now Smilie
# 5  
Old 06-20-2008
the thing is the last column of the file test is special, it may have something like "aaMM", which will be YYMM after the replacement, or "aaYY", which will be YYYY after the replacement, we can not just replace YY$ back to aa here.

Quote:
Originally Posted by robotronic
With GNU sed you may try something like this:

Code:
sed "s/\baa\b/YY/g; s/YY$/aa/" input_file.txt

No better ideas... For now Smilie
# 6  
Old 06-20-2008
This is very specific to the sample text provided and assumes that word boundary support, i.e. \b or \<, is not available in your version of sed.
Code:
$ sed -e 's/ aa$/ ++/' -e 's/aaa/+++/g' -e 's/aa/YY/g' -e 's/++/aa/' -e 's/+++/aaa/g' file
YY YY YY YY aa
c b c d YY a
e f ff mm aaa aaa 123
$

# 7  
Old 06-21-2008
Code:
sed -r 's/\baa /YY /g' file

Seems to produce the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

4. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

5. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

6. What is on Your Mind?

Too Hot Here

Officially, we had high of 96°F with a relative humidity of 52% here in Rockville Md today. But my car's thermometer said the outside temp was 110° when I first got in this afternoon. After driving for 30 minutes it cooled down to 103°. I actually have a nasty burn on my hand just from leaning... (19 Replies)
Discussion started by: Perderabo
19 Replies

7. Shell Programming and Scripting

Hot Keys

I am a new user, using Unix in a DOS window. Can I set up Hot Keys to run a script? Example - A12.1.13.15 aaaaBbbbCccc Thank you, cwtlr (8 Replies)
Discussion started by: cwtlr
8 Replies
Login or Register to Ask a Question