Visit Our UNIX and Linux User Community


Selective replace and delete


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Selective replace and delete
# 1  
Old 10-19-2015
Selective replace and delete

Hi
My input file looks like this:
Code:
>BAHMI01000090.1 Details of the shopping list  9800 item00090, whole set of listed artifacts and objects
>BAHMI01050012.1 Details of the shopping list  9800 item02310, whole set of listed artifacts and objects
>BAHMI01070078.1 Details of the shopping list  9800 item03340, whole set of listed artifacts and objects
>BAHMI01070121.1 Details of the shopping list  9800 item10010, whole set of listed artifacts and objects
>BAHMI01080450.1 Details of the shopping list  9800 item19000, whole set of listed artifacts and objects

Requird output:
Code:
>product00090
>product02310
>product03340
>product10010
>product19000

I tried doing this in 2 steps :

step 1 : using

Code:
sed 's/BAHMI01* Details of the shopping list  9800 item/product/g' inputfile> outputfile

I was expecting a result like

Code:
>product00090, whole set of listed artifacts and objects
>product02310, whole set of listed artifacts and objects
>product03340, whole set of listed artifacts and objects
>product10010, whole set of listed artifacts and objects
>product19000, whole set of listed artifacts and objects

But it didnt work.


Step2 was to use :

Code:
awk '/^>product/ {$0=$1}1' outputfile> outputfile2

But both my commands didn't yield required result

Could someone suggest?
# 2  
Old 10-19-2015
Code:
perl -ne '/item(\d*),/ and print ">product$1\n"' sonia102.file

or
Code:
sed 's/BAH.*item\([0-9]\{5\}\),.*$/product\1/' sonia102.file


Last edited by Aia; 10-19-2015 at 01:30 AM..
# 3  
Old 10-19-2015
Here's a version with sed and awk:
Code:
awk '{print ">" $8}' input.txt | sed -e 's/,//g' -e 's/item/product/g' > output.txt

Since your data looks to be in defined columns, you can used awk to grab column 8 and prepend a ">" to it. This will give you something like ">item#####,". You can then use sed to remove the trailing "," and replace "item" with "product".
# 4  
Old 10-19-2015
Code:
 awk '$8 ~ /item/ {print ">product" substr($8, 5, 5)}' sonia102.file

or
Code:
awk 'match($8, /[0-9]{5}/) {print ">product" substr($8, RSTART, RLENGTH)}' sonia102.file


Last edited by Aia; 10-19-2015 at 04:52 AM..
# 5  
Old 10-19-2015
There is no need to use both sed and awk. Here are a couple of additional ways to use either one of them to do the entire job:
Code:
sed -e 's/.*item/>product/' -e 's/,.*//' inputfile> outputfile2

or:
Code:
awk -F'.*item|,.*' '{print ">product" $2}' inputfile> outputfile2

with your sample inputfile, both of the above write:
Code:
>product00090
>product02310
>product03340
>product10010
>product19000

into outputfile2.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk if you want to use awk instead of sed.
# 6  
Old 10-19-2015
The only thing that is missing in your sed script in post#1 sed 's/BAHMI01* Details of the shopping list 9800 item/product/g' is the dot as the wild card char:
Code:
sed 's/BAHMI01.* Details of the shopping list  9800 item/product/' file
>product00090, whole set of listed artifacts and objects
etc...

(the g flag is not needed)

Add Don Cragun's ;s/,.*$// proposal to remove the unneeded rest of the line:
Code:
sed 's/BAHMI01.* Details of the shopping list  9800 item/product/;s/,.*$//' file
>product00090
etc...

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #961
Difficulty: Medium
SQL MammyJammer was a computer worm that caused a denial of service on some Internet hosts and dramatically slowed down general Internet traffic in 2003.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SQL selective replace

Hi I have a table which looks like this id | name | length | clone | null 1 | string 1 | 12345643 | string 1 | NULL | 2 | string 2 | 2345612 | string 2 | NULL | 3 | string 3 | 3421556 | string 3 | NULL | 4 | string 4 | 1236742 | string 4 | NULL | 5 | string 5 | 2312677 | string 5 | NULL |... (2 Replies)
Discussion started by: rsi.245
2 Replies

2. Shell Programming and Scripting

Selective Replace awk column values

Hi, I have the following data: 2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE" 32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Discussion started by: sdohn
3 Replies

3. UNIX for Dummies Questions & Answers

Selective delete in SQL

Hi All This might be a weird query but its related to deleting specific details in database. Bascially I had built a database using a set of files seq1 of 300 mb seq2 of 200 mb seq3 of 350 mb seq4 of 300 mb and after building the database i realized that i didn't need the whole data.... (6 Replies)
Discussion started by: sonia102
6 Replies

4. UNIX for Dummies Questions & Answers

Selective replace

i have a large sequence of format sat_1_g3_g_0_8540 . A 1 15501 . . . ID=sat_1_g3_g_0_8540;parentName=sat_1_g3_g_0_8540;Al=sat_1_g2_g_0_8540; sat_1_g3_g_2_8510 . C 1 25501 . . . ... (11 Replies)
Discussion started by: siya@
11 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Selective delete command

Hi All. I didnt know how to put this question as but i want to delete my values following string_X but need to retain the data in it. I hope the following might help me in conveying my doubt. My sequence looks like this. >string_1 CAJW010000001... (3 Replies)
Discussion started by: sonia102
3 Replies

6. Shell Programming and Scripting

Script to replace/delete lines in C program

I am under the gun on a project and am not very good at scripting. I have to make a modification to thousands of C programs to basically replace the #pragma statement. I don't want to have to do it manually. Here is an example of what I need done. Any help would be greatly appreciated. I... (5 Replies)
Discussion started by: jclanc8
5 Replies

7. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

8. Shell Programming and Scripting

RegEx - selective delete around a pattern

I need RegEx to delete text block delimited by "^--" and "^request saved" if the block contained the pattern "FAILED:No air,rail,hotel or car" in the following. Many thanks in advance! company_id=9292 queue_id=72 internationalOnly=0 Building XML... ABC123 Adding passenger first=First ... (1 Reply)
Discussion started by: roshansharma
1 Replies

9. Shell Programming and Scripting

replace/delete odd occurance

how to delete/replace the the odd occurance of a pattern say newline character "\n" a,b,c,d e,f g, h, i, j, k, l m,n 1, 2, 3, 4, 5, 6 1, 3, 4, 5, 6, 7 the output should be a,b,c,de,f g, h, i, j, k, lm,n 1, 2, 3, 4,5, 6 1, 3, 4, 5, 6, 7 (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

10. UNIX Desktop Questions & Answers

Trying to delete a directory pattern-selective deletion

Trying to delete a directory or a file using a pattern-selective deletion (using * and ? ) (4 Replies)
Discussion started by: herberwz
4 Replies

Featured Tech Videos