justaguy
justaguy

Reputation: 3022

search lines in bash for specific character and display line

I am trying to write search a string in bash and echo the line of that string that contains the + character with some text is a special case. The code does run but I get both lines in the input file displayed. Thank you :)

bash

#!/bin/bash

printf "Please enter the variant the following are examples"
echo " c.274G>T or c.274-10G>A"

printf "variant(s), use a comma between multiple: "; IFS="," read -a variant

for ((i=0; i<${#variant[@]}; i++))
 do printf "NM_000163.4:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/input.txt
done
awk '{for(i=1;i<=NF;++i)if($i~/+/)print $i}' input.txt
echo "$i" "is a  special case"

input.txt

NM_000163.4:c.138C>A
NM_000163.4:c.266+83G>T

desired output ( this line contains a + in it)

NM_000163.4:c.266+83G>T is a special case

edit:

looks like I need to escape the + and that is part of my problem

Upvotes: 0

Views: 39

Answers (2)

karakfa
karakfa

Reputation: 67467

you can change your awk script as below and get rid of echo.

$ awk '/+/{print $0,"is a special case"}' file

NM_000163.4:c.266+83G>T is a special case

Upvotes: 1

Lars Fischer
Lars Fischer

Reputation: 10129

As far as I understand your problem, you can do it with a single sed command:

sed -n '/+/ {s/$/is a special case/ ; p}' input.txt

On lines containing +, it replaces the end ($) with your text, thus appending it. After that the line is printed.

Upvotes: 1

Related Questions