LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2012, 02:41 AM   #1
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Rep: Reputation: 19
replacing list of numbers from a file


I have a rather long file with ids (integer) in it, under the circumstances if I do not do some processing they appear on comma separated lists
about 10 per line, for example:
1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010

now I want to replace these ids one by one with new ones and I can allocate
the new ids into a separate list, for example:
1001, 2001
1002, 2002
etc.
now the numbers are not running numbers and not in some order or as in these examples.

The question is how can I perform the replacement in file 1 taking the new ids from file 2 one by one. There is a chance that an id may occur more in the file 1, but it is highly unlikely so it could be limited to replacement of first occurence.

How should I go on? can I pipe it through sed somehow or should I make a short program? I can still modify the format of file 2 to for example:
/1001/,/2001/
this I could maybe pipe into sed as a variable, but at this moment my brain fails me - so help would be appreciated.
 
Old 06-24-2012, 03:14 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
It is not clear to me exactly what you need to do---please post a sample of file 1, a sample of file 2, and sample of what the final should look like.
 
Old 06-24-2012, 03:30 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
If the file format is not too sensitive, you could get by with a pretty simple awk script.
Code:
#!/usr/bin/awk -f
BEGIN {
    # Accept any newline convention; ignore leading and trailing whitespace.
    RS = "[\t\v\f ]*(\r\n|\n\r|\r|\n)[\t\v\f ]*"

    # Fields are separated by commas, with optional whitespace.
    FS = "[\t\v\f ]*,[\t\v\f ]*"

    # For output, use newlines and commas only.
    ORS = "\n"
    OFS = ","

    # First file specifies replacements.
    file = 0
}

# Increase file number when the first record is seen.
(FNR == 1) { file++ }

# Record replacements only from the first file.
(file == 1 && NF >= 2) { replace[$1] = $2 }

# All other files:
(file > 1 && NF > 0) {
    for (i = 1; i <= NF; i++) {
        value = $i
        if (value in replace)
            value = replace[value]
        if (i < NF)
            printf("%s%s", value, OFS)
        else
            printf("%s%s", value, ORS)
    }
}
You specify two or more input files to the script. The first one contains the replacements: old values in the first column, and replacement values in the second column.

The rest of the input files will be split at commas (with whitespace around a comma removed). The values are only replaced if the entire field matches. On output, it uses commas (OFS) between fields, and newline (ORS) to end each line.

Given first input file containing
Code:
1001, 2001
1002, 2002
and second input file containing
Code:
1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010
the script will output
Code:
2001,2002,1003,1004,1005,1006,1007,1008,1009,1010
 
1 members found this post helpful.
Old 06-24-2012, 04:38 AM   #4
Jykke
Member
 
Registered: Sep 2005
Posts: 201

Original Poster
Rep: Reputation: 19
Thanks that is exactly what I need - well have to try it out first, but according to your output it
seems to do the job!

Thanks!
 
  


Reply



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
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 09:51 AM
Replacing numbers in bash scripts bluesmodular Programming 1 05-26-2010 12:04 AM
Replacing a numeric string with a sequence of numbers ziggy25 Linux - Newbie 13 12-03-2009 10:18 AM
Replacing selected columns by Serial numbers incremently raghu123 Programming 8 08-25-2008 02:27 AM
bash scripting: loop over a file, replacing two decimal numbers frankie_DJ Programming 2 04-30-2007 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 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