sed A Value In A Column

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed A Value In A Column
# 1  
Old 04-04-2017
sed A Value In A Column

trying to replace a specific value in a column with sed. this is what I have but can't get it to work, replace dog with kitty if dog is in the 3rd column and bunny in the 1st column if bird is there
Code:
bird lizard fish
bird lizard dog
bird lizard fish

Code:
sed '/bird/s/^/bunny/1;'/dog/s/^/kitty/3' file

trying different iterations but just can't seem to get it to work

this would be the output
Code:
bunny lizard fish
bunny lizard kitty
bunny lizard fish

# 2  
Old 04-04-2017
sed might not be the optimal tool for this, but try
Code:
sed 's/^bird/bunny/;s/\([^ ]* [^ ]* \)dog/\1kitty/' file
bunny lizard fish
bunny lizard kitty
bunny lizard fish

# 3  
Old 04-04-2017
it works but I don't understand why
# 4  
Old 04-04-2017
In it it simplest form using anchors ( ^ and $ ):
Code:
sed 's/^bird/bunny/; s/dog$/kitty/' file

or depending of input variation, a little better, also using spaces to mark the other side of the word boundary:
Code:
sed 's/^bird /bunny /; s/ dog$/ kitty/' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. UNIX for Dummies Questions & Answers

Printing a particular column using SED

Hi, i want to display only the particular column using SED command. For example, ps -ef|grep ash |sed -n '1p'|cut -d ' ' -f2   this gives 29067 ps -ef|grep ash |sed -n '1p'|awk '{print $2}'    this also gives the same  in the same way i need the solution using sed. Please... (4 Replies)
Discussion started by: pandeesh
4 Replies

5. Shell Programming and Scripting

use sed on only column $3 with awk

I am using the command ls -lhB --group-directories-first --no-group --file-type --time-style=+%D $@ | sed 's/AlphaLexman/ALM/g' to replace my username with my initials (to keep the width shorter), but if my username is in the filename such asreport_by_AlphaLexman.txtit becomesreport_by_ALM.txt ... (2 Replies)
Discussion started by: AlphaLexman
2 Replies

6. 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

7. UNIX for Dummies Questions & Answers

sed changes by column

Is it possible to make changes with the sed command in a specific column only? If yes, how? If not, how could I do that instead? Thanks! ---------- Post updated at 02:01 PM ---------- Previous update was at 01:39 PM ---------- e.g. Group1 Group2_3_1 1_2_3_4_5_ I would like... (8 Replies)
Discussion started by: danieladna
8 Replies

8. Shell Programming and Scripting

Can SED Search By Column?

Hi all, I am trying to search for a keyword in the fourth column of a massive carrot(^) delimited file and unfortunately I cannot use AWK (which would have been ideal). Can SED (or maybe even GREP) perform a search like this? (12 Replies)
Discussion started by: Korn0474
12 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Change one column using sed??

Is there way to use sed to change only one occurence in a colon separated line? I would like to change a file from *:*:rex:rex *:*:simon:rex to *:*:rex:mabry *:*:simon:rex (2 Replies)
Discussion started by: rexmabry
2 Replies
Login or Register to Ask a Question