LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-03-2015, 04:15 PM   #1
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Rep: Reputation: 30
Find and replace in one file with contents in a separate file


I am working with Volusion and want to change categories for products using libreoffice or by using bash as my info is in csv files. The problem is that my products download file shows categories only by id number (ie. 1, 2, 3, 4)

But I can download a separate categories file that has the id's in one column and the category name in another column.

I want to change the category id's in one file with the actual category name by using the data from the second file.

Here's an example:

---Product File---

Widget1 $19.95 1,3,5 ---Item is in 3 categories

Widget2 $24.95 2,4 ---this means it's in 2 categories

---Category File---

1 Books

2 Watches

3 Desks

4 Chairs

5 Doors

Hopefully this is possible to have my products file show up like this:

Widget1 $19.95 1,3,5 Books,Desks,Doors

Widget2 $24.95 2,4 Watches,Chairs

Is this even possible using libreoffice? If not, could I do this find and replace idea with a simple bash script?

I have 11,000 items and 1200 categories. This will save me a lot of time.
 
Old 09-03-2015, 08:07 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,831

Rep: Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007Reputation: 4007
This seems to be a common sort of request - nothing in the "Similar threads" below ?.

Logic should be pretty straightforward - read the category file into an array, then use the numbers in the products records to index into the arrays and append the value. Personally I'd use awk.
 
Old 09-03-2015, 08:42 PM   #3
Aia
Member
 
Registered: Jun 2006
Posts: 66

Rep: Reputation: 21
Please, try this and see if it does what you want.
Code:
awk '
FNR==NR {
    cat[$1]=$2
    next
}
{
    n=split($3, t, ",")
    s=cat[t[1]]
    for(i=2; i<=n; i++)
        s=s "," cat[t[i]]
    print $0, s
}' category.file product.file
Code:
Widget1 $19.95 1,3,5 Books,Desks,Doors
Widget2 $24.95 2,4 Watches,Chairs

Last edited by Aia; 09-03-2015 at 08:46 PM.
 
Old 09-04-2015, 04:39 PM   #4
xmrkite
Member
 
Registered: Oct 2006
Location: California, USA
Distribution: Mint 16, Lubuntu 14.04, Mythbuntu 14.04, Kubuntu 13.10, Xubuntu 10.04
Posts: 554

Original Poster
Rep: Reputation: 30
Hey, that got me to it. Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Move file contents to another file with bash script (find, mimetype and cat) _furrycat_ Linux - Newbie 1 10-06-2013 11:12 AM
Shell script to search one file for contents of another and replace text? kmkocot Linux - Newbie 6 10-28-2011 02:09 PM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
Script to check a file and replace some of the contents tara Programming 2 03-22-2009 09:15 PM
Replace contents of a file shiroh_1982 Linux - Newbie 3 05-25-2006 07:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration