Grep to remove and add specified characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep to remove and add specified characters
# 1  
Old 05-16-2014
Grep to remove and add specified characters

I have the following type of 2 column file:

Code:
motility	-
role	-
supplementation	-
age	b
ancestry	b
purity	b
recommendation	b
serenity	b
unease	b
carving	f
expansion	f

I would like to print only certain sections of the file depending on the value of the second column.

For instance, I only want to print those lines that have "-" as the value of the second column.

However, in the output, I also want to add the following characters to each line end, for a desired result of:

Code:
motility-n
role-n
supplementation-n

Is there a
Code:
grep

that allows me to do this?
Can anyone help me with this issue?
# 2  
Old 05-16-2014
try
Code:
awk '/-/ {print $0"n"}' filename

OR
Code:
awk '{gsub (/^ */,"",$1); print}' file| awk '/-/ {print $0"n"}'

output
Code:
motility -n
role -n
supplementation -n


Last edited by Makarand Dodmis; 05-16-2014 at 05:58 AM..
# 3  
Old 05-16-2014
Try also
Code:
awk '$2==SRCH {print $1 SRCH "n"}' SRCH="-" file
motility-n
role-n
supplementation-n

# 4  
Old 05-16-2014
No. The grep utility searches for lines matching a regular expression, it does not change/replace text on matched lines.
Try:
Code:
sed -n 's/      -$/-n/p' filename

If it isn't obvious, the 1st part of the sed substitute command contains a literal tab character, not spaces (<s></><tab><minus-sign><dollar-sign></>...).

Or to make Makarand Dodmis' awk script work, you could try:
Code:
awk -F '\t' '$2 == "-" {print $1"-n"}' filename

Note that if you try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-16-2014
how about
Code:
awk '/-/ {print $1"-n"}' file

# 6  
Old 05-16-2014
That would still be vulnerable to "-" anywhere on the line.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-16-2014
right
Code:
awk '$2 ~ /-/ {print $1"-n"}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove first 2 characters and last two characters of each line

here's what im trying to do. i have a file containing lines similar to this: data.txt: 1hsRmRsbHRiSFZNTTA1dlEyMWFkbU5wUW5CSlIyeDFTVU5SYjJOSFRuWmpia0ZuWXpKV2FHTnRU 1lKUnpWMldrZFZaMG95V25oYQpSelEyWTBka2QyRklhSHBrUjA1b1kwUkJkd3BOVXpWM1lVaG5k... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

Grep to remove non-ASCII characters

I have been having an encoding problem that I need to solve. I have an 4-column tab-separated file: I need to remove all of the lines that contain the string 'vis-à-vis' achiever-n vis-à-vis+ns-j+vp oppose-v 1 achiever-n vis-à-vis+ns-the+vg assess-v 1 administrator-n ... (4 Replies)
Discussion started by: owwow14
4 Replies

3. Shell Programming and Scripting

How to remove unprintable characters?

Hi Gurus, when I run following command to split one line file to multiple lines, it stopped because of hitting un-printable special charactor. awk -v L="$2" '{for (i=1; i<=length($0); i+=L) print substr($0, i, L)}' "$1" > "$1"_split I use cat -A, I can see one un-printable charactor M-^T. how... (1 Reply)
Discussion started by: ken6503
1 Replies

4. Shell Programming and Scripting

remove first few characters from each line

Hi, I have a file with lines like below. I need to remove first few characters from each line until a date format is found. 05/06/12 20:47:02 GUMGUY@98.192.174.74{42B42A72AC955F5926621273E3A15059.tomcat2}TP-Processor15 LogExchUsage: ERROR: 05/06/12 20:47:02... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

5. UNIX for Dummies Questions & Answers

How do I remove ^M characters with VI

I have a file with all kinds of ^M at the end of each line. How the heck can these be removed? I tried a global search and replace, but it doesn't seem to work. Thanks! (8 Replies)
Discussion started by: HmmBerger
8 Replies

6. Shell Programming and Scripting

Remove characters other than ISO8859-1

Hi please help in writing a script for replacing all the non-iso8859-1 characters to question marks. I need a pattern of this kind "sed s/<non-iso char range>/?/g < ipfile > opfile" Please help me in this. (2 Replies)
Discussion started by: rprajendran
2 Replies

7. Shell Programming and Scripting

grep or sed. How to remove certain characters

Here is my problem. I have a list of phone numbers that I want to use only the last 4 digits as PINs for something I am working on. I have all the numbers in a file but now I want to be removed all items EXCEPT the last 4 digits. I have seen sed commands and some grep commands but I am... (10 Replies)
Discussion started by: Sucio
10 Replies

8. UNIX for Advanced & Expert Users

remove characters

hi i have a file with these strings: 123_abc_X1116990 how to get rid of 123_abc_ and keep only X1116990? I have columns of these: 123_abc_X1134640 123_dfg_X1100237 123_tyu_X1103112 123_tyui_X1116990 thx (5 Replies)
Discussion started by: melanie_pfefer
5 Replies

9. Shell Programming and Scripting

remove space characters

hello I have this output ifspeed 100000000 ifspeed 100000000 collisions 413 collisions 10 duplex full duplex ... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. UNIX for Dummies Questions & Answers

How to remove Characters before '~'

Hi, I am having a file which contains records as follows: DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131 DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131 DETAIL_KEY~12344|ACTIVE_PASSIVE~Y|AVG_SIZE_OF_RESPONSE~123123131... (4 Replies)
Discussion started by: Amey Joshi
4 Replies
Login or Register to Ask a Question