Help with file editing while keeping file format intact


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with file editing while keeping file format intact
# 8  
Old 12-22-2010
I have one Extracted CSV file and one param file. Param file has a list of values and their mapping values. I want to replace one of the field in CSV with the matching mapping value in param file.
Example:
Code:
CSV: 123,   ABCD,  487274
        324,   ASKJ,  472844
Param file:   export ABCD=20
                 export XYZ=44

ABCD should get replaced with 20.

I am reading CSV as
Code:
cat ${DATA_FILE} | while read LINE

Note: I can change format of param file if require.
Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 12-22-2010 at 01:14 PM..
# 9  
Old 12-22-2010
If CSV file is has one space after comma (That's how above CSV file looks like), then following one line awk should do the needful.
Code:
awk -F'( |=)' 'NR==FNR{a[$2]=$3;next;}{FS=", ";for(i=1;i<=NF;i++) if(a[$i]) $i=a[$i];print}' OFS=", " Param_File CSV_File

If field no/column no in CSV to be replaced is fixed.. say 2nd (Only values in 2nd columns are supposed to be replaced then)
Code:
awk -F'( |=)' 'NR==FNR{a[$2]=$3;next;}{FS=", ";if(a[$2]) $2=a[$2];print}' OFS=", " Param_File CSV_File

If there is no space in CSV file after comman, these commands need to be modified a little.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

2. Shell Programming and Scripting

Remove duplicates by keeping the order intact

Hello friends, I have a file with duplicate lines. I could eliminate duplicate lines by running sort <file> |uniq >uniq_file and it works fine BUT it changes the order of the entries as it we did "sort". I need to remove duplicates and also need to keep the order/sequence of entries. I... (1 Reply)
Discussion started by: magnus29
1 Replies

3. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies

4. Shell Programming and Scripting

Remove last few characters in a file but keeping Header and trailer intact

Hi All, I am trying write a simple command using AWK and SED to this but without any success. Here is what I am using: head -1 test1.txt>test2.txt|sed '1d;$d' test1.txt|awk '{print substr($0,0,(length($0)-2))}' >>test2.txt|tail -1 test1.txt>>test2.txt Input: Header 1234567 abcdefgh... (2 Replies)
Discussion started by: nvuradi
2 Replies

5. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

6. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

7. UNIX for Dummies Questions & Answers

sort by keeping the headings intact?

Hi all, I have a file with 3 columns separated by space. Each column has a heading. I want to sort according to the values in the 2nd column (ascending order). Ex. Name rank direction goory 0.05 --+ laby 0.0006 --- namy 0.31 -+- ....etc. Output should be Name rank direction laby... (3 Replies)
Discussion started by: Unilearn
3 Replies

8. HP-UX

Editing a UNIX file in Hexadecimal format

Hi, I am a newbie to the UNIX world. I am asked to edit the file in hexadecimal format and save it. Later I should be able to print the file in char mode. please anyone tell me how to do that mostly using VI editor. (1 Reply)
Discussion started by: vkudire
1 Replies

9. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

10. Shell Programming and Scripting

Keeping the format ...

Hi all, Am trying to execute a loop but having some troubles... Files that will be query'd use the Julian date (eg: cpu032, cpu365) in their naming convention. I'm a little lost how to maintain the three character format of the numeric portion of the file name while cycling backwards(or... (13 Replies)
Discussion started by: Cameron
13 Replies
Login or Register to Ask a Question