Exact match using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exact match using sed
# 1  
Old 01-31-2014
Exact match using sed

I would like replace all the rows in a file if a row has an exact match to number say 21 in a tab delimited file. I want to delete the row only if it has 21 any of the rows but it should not delecte the row that has 542178 or 563421.

I tried this
Code:
sed '/\<21\>/d' ./inputfile > output.txt

but it is not working. Please let me know what I am missing here.

Thanks
# 2  
Old 01-31-2014
Do you mean 21 in any of the TAB-delimited columns? Can you show us a sample of the input file? Does it have to be sed?
# 3  
Old 01-31-2014
Exact match using grep

Hope, it mat useful to you.

Code:
grep -vw "21" input_file > output_file

Please post two records which contains 21 and 542178, then I can provide you the sed command.
# 4  
Old 01-31-2014
Following is an example of the input file
Code:
543 1 2 5 21
5421 5 9 2
678 7 8 10
78921 9 10 11 21
78 0 23 2 4
9821 3 4 2 21
8943 7 8 10

Desired out put
Code:
5421 5 9 2
678 7 8 10
78 0 23 2 4
8943 7 8 10

Hope this helps

---------- Post updated at 10:07 PM ---------- Previous update was at 10:06 PM ----------

It doesn't have to be sed, awk will be fine
# 5  
Old 02-01-2014
In your sample input, the value you're looking for only appears in the last field.

Here are a couple of awk scripts. The first script only looks for the desired value in the last field of each input line; the second script looks for the desired value in any filed:
Code:
#!/bin/ksh
search_value=${1:-21}
echo 'Checking last field only:'
awk -v sv="$search_value" '$NF != sv' file
printf '\nChecking every field:\n'
awk -v sv="$search_value" '
{       for(i = 1; i <= NF; i++)
                if($i == sv)
                        next
        print
}' file

If you want to try this on a Solaris/SunOS System, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk. This script was tested using a Korn shell, but will also work with any other shell that recognizes basic POSIX shell syntax (such as bash).

If you make the above script an executable file named tester and the file named file contains your sample input:
Code:
543 1 2 5 21
5421 5 9 2
678 7 8 10
78921 9 10 11 21
78 0 23 2 4
9821 3 4 2 21
8943 7 8 10

the output produced by executing either of the commands:
Code:
./tester
    or
./tester 21

will be:
Code:
Checking last field only:
5421 5 9 2
678 7 8 10
78 0 23 2 4
8943 7 8 10

Checking every field:
5421 5 9 2
678 7 8 10
78 0 23 2 4
8943 7 8 10

and the command:
Code:
./tester 2

will produce the output:
Code:
Checking last field only:
543 1 2 5 21
678 7 8 10
78921 9 10 11 21
78 0 23 2 4
9821 3 4 2 21
8943 7 8 10

Checking every field:
678 7 8 10
78921 9 10 11 21
8943 7 8 10

# 6  
Old 02-01-2014
Code:
 sed -i "/ 21$/d" file

it will also work fine for your data, means required value is at last column.

Don, please correct me if my assumption was wrong.
# 7  
Old 02-01-2014
Quote:
Originally Posted by srini matta
Code:
 sed -i "/ 21$/d" file

it will also work fine for your data, means required value is at last column.

Don, please correct me if my assumption was wrong.
Your assumption is correct if the fields are delimited by spaces (as was shown in the sample input). But, the text describing the input said the fields were tab delimited.

With a version of sed that adheres to the POSIX requirements, that would be:
Code:
sed '/<tab>21$/d' file

where <tab> is replaced by a literal tab character. Some versions of sed also recognize \t as a way to specify a tab character in a sed address expression, but that behavior is not portable. To delete any line containing 21 as the value in any (space delimited) field on a line, you could use something like:
Code:
sed '/^21$/d;/^21 /d;/ 21$/d;/ 21 /d' file

where the 1st command deletes 21 on a line by itself, the 2nd deletes lines with 21 as the 1st field, the 3rd deletes lines with 21 as the last field, and the 4th deletes lines with 21 as a field other than the 1st and last fields.

I find the awk script easier to understand even though it is longer than the sed script; but that is just a personal preference. And, the awk script works with any combination of spaces and tabs as delimiters. An equivalent sed script would be:
Code:
sed '/^21$/d;/^21[ <tab>]/d;/[ <tab>]21$/d;[ <tab>]21[ <tab>]/d' file

where each occurrence of <tab> is replaced by a literal tab character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate. This is in reference to this thread -> https://www.unix.com/shell-programming-and-scripting/267630-extract-between-two-exact-matched-strings.html I have data.txt as... (11 Replies)
Discussion started by: mohtashims
11 Replies

3. UNIX for Dummies Questions & Answers

sed Exact Match when Dot is present

I am trying to replace exact word from my text. I know using the angled brackets does the trick. But it is not working when there is a dot in the text. echo "Bottle BottleWater Bottle Can" | sed 's/\<Bottle\>//g' BottleWater CanBut if my data has a dot or hash in it, it replaces all the... (10 Replies)
Discussion started by: grep_me
10 Replies

4. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

5. Shell Programming and Scripting

grep and sed exact match questions

This post was previously mistaken for homework, but is actually a small piece of what I working on at work. Please answer if you can. QUESTION1 How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (2 Replies)
Discussion started by: thibodc
2 Replies

6. UNIX for Dummies Questions & Answers

grep and sed exact match questions

This was mistaken as homework in a different forum, but is not. These are questions that are close to what I am trying to do at work. QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. Shell Programming and Scripting

sed to match only exact string only in all occurences

Dear Friends, Anybody knows how to match exact lines only in multilinear. Input file: apple orange orange apple apple orange Desired output: fruit orange apple fruit i used the command (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

10. UNIX for Dummies Questions & Answers

using sed or grep to find exact match of text

Hi, Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only) 1 Hello world fantasy. 2 Hello worldfuntastic. 3 Hello world wonderful. I would like to get all those lines of text that... (5 Replies)
Discussion started by: risk_sly
5 Replies
Login or Register to Ask a Question