String copy


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String copy
# 1  
Old 03-25-2011
String copy

Hi guys

I've got two columns, PRODUCT and BRAND, the Brand column currently has the first word of each product, I've acheived this by using SED to copy the first word of the PRODUCT column, however you run into trouble when the brand has more than one word, i.e. 'Weight Watchers'.

Is there a way I can do a search for all the products that have 'Weight Watchers' in the title and then copy the string 'Weight Watchers' to the brand column?

Weight Watchers Apricot Jam Reduced Sugar Weight
All Natural Peanut Butter & Co Crunch Time All
Streamline Reduced Sugar Apricot Jam Streamline

Many thanks
# 2  
Old 03-25-2011
give the sample, and your expect output.
# 3  
Old 03-25-2011
Sorry if the above isn't clear, hopefully this is easier to understand.

Sample


Product Brand
Weight Watchers Apricot Jam Reduced Sugar Weight

Expected Output

Product Brand
Weight Watchers Apricot Jam Reduced Sugar Weight Watchers

Thanks
# 4  
Old 03-25-2011
Try..
Code:
awk '/Weight Watchers/{print $0 FS $2;getline}1' inputfile > outfile

# 5  
Old 03-25-2011
Quote:
Originally Posted by michaelrozar17
Try..
Code:
awk '/Weight Watchers/{print $0 FS $2;getline}1' inputfile > outfile

Hi Michael, thanks for you kind help, that's really close but it's only doing the change on every other line, I'll try to explain:

Here's my input file, I've left some of the other columns in, didn't seem to make a difference, sorry if it seems a mess, can't see to get the tabs to show up on this forum.
Code:

Castillo d'Canena Extra Virgin Arbequina Olive Oil (250ml)    8            10000        Castillo
Castillo d'Canena Extra Virgin Picual Olive Oil (250ml)    8            10000        Castillo
Farchioni Il Casolare Olive Oil Extra Virgin (1L)    8            10000        Farchioni
Filippo Berio Extra Virgin Olive Oil (250ml)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil (500ml)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil (750ml)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil (1L)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil Special Selection (250ml)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil Tin (1L)    8            10000        Filippo 
Filippo Berio Gusto Fruttato Extra Virgin Olive Oil (500ml)    8            10000        Filippo 
Filippo Berio Mild & Light Olive Oil (250ml)    8            10000        Filippo 
Filippo Berio Mild & Light Olive Oil (500ml)    8            10000        Filippo 
Filippo Berio Mild & Light Olive Oil (1L)    8            10000        Filippo 

On this I ran:

Code:
awk '/Filippo Berio/{print $0 FS $2;getline}1' test1.txt > test2.txt

And the output:

Code:
Castillo d'Canena Extra Virgin Arbequina Olive Oil (250ml)    8            10000        Castillo
Castillo d'Canena Extra Virgin Picual Olive Oil (250ml)    8            10000        Castillo
Farchioni Il Casolare Olive Oil Extra Virgin (1L)    8            10000        Farchioni
Filippo Berio Extra Virgin Olive Oil (250ml)    8            10000        Filippo  Berio
Filippo Berio Extra Virgin Olive Oil (500ml)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil (750ml)    8            10000        Filippo  Berio
Filippo Berio Extra Virgin Olive Oil (1L)    8            10000        Filippo 
Filippo Berio Extra Virgin Olive Oil Special Selection (250ml)    8            10000        Filippo  Berio
Filippo Berio Extra Virgin Olive Oil Tin (1L)    8            10000        Filippo 
Filippo Berio Gusto Fruttato Extra Virgin Olive Oil (500ml)    8            10000        Filippo  Berio
Filippo Berio Mild & Light Olive Oil (250ml)    8            10000        Filippo 
Filippo Berio Mild & Light Olive Oil (500ml)    8            10000        Filippo  Berio
Filippo Berio Mild & Light Olive Oil (1L)    8            10000        Filippo


Last edited by Franklin52; 03-25-2011 at 10:21 AM.. Reason: Please use code tags
# 6  
Old 03-25-2011
Could you change as below and try..
Code:
awk '/Filippo Berio/{print $0 FS $2;next}1' inputfile > outfile

This User Gave Thanks to michaelrozar17 For This Post:
# 7  
Old 03-25-2011
Quote:
Originally Posted by michaelrozar17
Could you change as below and try..
Code:
awk '/Filippo Berio/{print $0 FS $2;next}1' inputfile > outfile

Ah ha ----- perfect :-) You just saved me lots of manual find and replace.

---------- Post updated at 04:38 AM ---------- Previous update was at 04:28 AM ----------

Hi again

I've got 40+ of these to do, what's the best way to go about putting all 40 in a script, e.g.
Code:
awk '/Filippo Berio/{print $0 FS $2;next}1' test1.txt > test2.txt
awk '/Sun Grown/{print $0 FS $2;next}1' test1.txt > test2.txt

The above won't work obviously, however with AWK can you work on the input file alone so you don't have to output to a second file?

I also tried putting two command on the same line but this causes errors in other products.
Code:
awk '/Filippo Berio/{print $0 FS $2;next}1','/Sun Grown/{print $0 FS $2;next}1' test1.txt > test2.txt

thanks

Last edited by Franklin52; 03-25-2011 at 10:22 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy a string to another file

OS version: RHEL 6.7 Shell : Bash I have a file like below. It has 500K lines. I want to extract TAG_IDs shown in single quote at the end to copied to another file. As if I had copied the TAG_IDs using block select (Column Select) in modern text editor $ cat file.txt UPDATE TAGREF SET... (9 Replies)
Discussion started by: John K
9 Replies

2. Shell Programming and Scripting

Copy last third char form string

HI Input A.txt ABC907 ABC907_1B_9 ABC985 ABC985_1A_9 ABC985 ABC985_1B_9 ABC985 ABC985_1C_9 ABC05037 ABC05037_1A_9 ABC05037 ABC05037_1B_9 Base of column 2 last third char. If It is A the 1,if B then 2 If C then 3 File B.txt ABC907 ABC907_1B_9 2 ABC985 ABC985_1A_9 1 ABC985... (8 Replies)
Discussion started by: asavaliya
8 Replies

3. Shell Programming and Scripting

Help on Script of Copy String from column

Hello, My DATA: PLOKIJ1234G 12 13 14 15 PLOKIJ1234E 12 13 14 15 PLOKIJ1234F 12 22 33 44 IJNUHB12346 55 66 77 88 IJNUHB12347 32 34 45 67 IJUHU345D 23 23 22 33 IJUHYG23E 11 24 23 23 IJUHYG23F 77 88 99 00 output: PLOKIJ1234 PLOKIJ1234G 12 13 14 15 PLOKIJ1234... (11 Replies)
Discussion started by: asavaliya
11 Replies

4. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

5. Programming

String copy in C language

Hello, I have a text file (FILE.txt) that contains the following information: userAxxx.name@email.com userA userBxxx.name@email.com userB userxxCx.name@email.com userC and more.... in scripting, I can easily do a grep and awk to store an email info into a string... (6 Replies)
Discussion started by: tqlam
6 Replies

6. Shell Programming and Scripting

Copy-paste string automatically

Hi i'm not really sure if it's possible or not in bash. Basically I want to convert a ssh key created with ssh-keygen to putty format. The problem is that puttygen doesn't have an option for supplying passphrase in batch mode so it must be entered manually. For sskey generated with ssh-keygen i use... (2 Replies)
Discussion started by: ktm
2 Replies

7. Shell Programming and Scripting

how to copy one string in ksh into another

Hi Does anybody know if there is a utility/command in ksh which would allow to copy/insert the contents of one string into certain positions of the other? for example: A=" ABCDEF " B="HHH" I need to insert contents of string "B" into string "A" from position 3 to 5, so... (3 Replies)
Discussion started by: aoussenko
3 Replies

8. Shell Programming and Scripting

find a string and copy the string after that

Hi! just want to seek help on this: i have a file wherein i want to find a string and copy the string after that and paste that other string to a new file. ex: TOTAL 123456 find "TOTAL" and copy "123456" and paste "123456" to a new file NOTE: there are many "TOTAL" strings on that... (12 Replies)
Discussion started by: kingpeejay
12 Replies

9. UNIX for Dummies Questions & Answers

Copy or Grep all text below a string

Hello, I am trying to copy all the text from a file below a search string... For example i want to grep all text below the word sure: UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! ... (2 Replies)
Discussion started by: aliaa2a
2 Replies

10. Shell Programming and Scripting

Copy / string.

I'm trying to get a script to copy a url then put it in a different place in the file. Example is currently the script goes to a site takes the urls on it and then puts them into an html file. Only thing is I want to make them into links. So currently lynx goes to the page takes out the urls.... (6 Replies)
Discussion started by: Paulw0t
6 Replies
Login or Register to Ask a Question