Search a pattern in a line and remove another pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a pattern in a line and remove another pattern
# 1  
Old 07-25-2013
Search a pattern in a line and remove another pattern

Hi,
I want to search a pattern in a text file and remove another pattern in that file.
my text file look like this

Code:
 0.000000  1.970000 F  303 -
1.970000  2.080000 VH VH +
2.080000  2.250000 VH VH +
2.250000  2.330000 VH L -
2.330000  2.360000 F  H +
2.360000  2.410000 L VL -
2.410000  2.450000 F  312 +

I want to search the line containing 'F' and remove signs of that line(+ or -)

Last edited by Don Cragun; 07-26-2013 at 01:38 AM.. Reason: add CODE tags
# 2  
Old 07-25-2013
Code:
awk '$3=="F"{gsub(/[+-]/,X)}1' file

# 3  
Old 07-25-2013
sreejith,
check it out..

Code:
$ awk '$3~/F/&&(($NF~/-/)||($NF~/\+/)){$NF="";print} ' file
0.000000 1.970000 F 303
2.330000 2.360000 F H
2.410000 2.450000 F 312

# 4  
Old 07-25-2013
@reveri
He does not say that he like to remove all other line, just remove -/+ of lines with F.
Also he does not specify what part of the line does contain the F, so it does not need to be at $3
Code:
awk '/F/{NF-=1}1' file

If its at $3 and F may be in combination with other letters, use:
awk '$3~"F"{NF-=1}1' file
# 5  
Old 07-25-2013
Jotne,
Thanks , understood the reqiurement now.

I tried your codes, looks like not working for me: not removing signs ( + - ) .os:hp-ux


Code:
$ awk '/F/{NF-=1}1' file
0.000000 1.970000 F 303 -
1.970000 2.080000 VH VH +
2.080000 2.250000 VH VH +
2.250000 2.330000 VH L -
2.330000 2.360000 F H +
2.360000 2.410000 L VL -
2.410000 2.450000 F 312 +

Code:
$ awk '$3~"F"{NF-=1}1' file
0.000000 1.970000 F 303 -
1.970000 2.080000 VH VH +
2.080000 2.250000 VH VH +
2.250000 2.330000 VH L -
2.330000 2.360000 F H +
2.360000 2.410000 L VL -
2.410000 2.450000 F 312 +

# 6  
Old 07-26-2013
Try this:
awk '/F/{NF=NF-1}1' file
or Yodas suggestion
# 7  
Old 07-26-2013
In HP-UX, you have to change NF and also force record reconstruction to get desired o/p:
Code:
awk '/F/{NF-=1;$1=$1}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk to search for pattern and remove line

I am an awk beginner and need help figuring out how to search for a number in the first column and if it (or anything greater) exists, remove those lines. AM11400012012 2.26 2.12 1.98 2.52 3.53 3.01 3.62 5.00 3.65 7.95 0.79 3.88 0.00 AM11400012013 3.39 2.29 ... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

7. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question