unable to add columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unable to add columns in a file
# 1  
Old 06-24-2010
unable to add columns in a file

Hi,

I am unable to add columns in a file using the below script.
Extract SiteID=1234 and PID=900010 add into the two columns in the file.

Input filename: customer_information_detail_900010_1234.csv

Input:

Code:
10,dravid,10000,5000,abc
20,sachine,20000,10000,bbc
30,raina,4000,1200,ccb

output:

Code:
10,dravid,10000,5000,abc,900010,1234
20,sachine,20000,10000,bbc,900010,1234
30,raina,4000,1200,ccb,900010,1234

Script information:

Code:
#!/bin/sh

#################################################################
# adding columns to file.
#################################################################

for file3 in `ls|grep Detail| grep csv`
do


########Fetching SiteID information example: 1234############
########Fetching PID information example: 900010############
########Filename example: customer_information_detail_900010_1234############

       SiteID=`echo $file3 |cut -d "." -f1 |awk -F[_-] '{print $(NF)}'`
       PID=`echo $file3 |cut -d "." -f1 |awk -F[_-] '{print $(NF-1)}'`
       filename=`echo $file3 |cut -d "." -f1`


       awk 'BEGIN {FS=OFS=","} {print $0,FILENAME,FILENAME}' $file3 > $filename".txt"
       sed s/$file3/$SiteID/g  $filename".txt" >temp.txt
       sed s/$file3/$PID/g  temp.txt >$file3
done

The script is failing beacuse the sed function is not allowing the parameters to replace the value.
i need to add two columns from the filename.Any help will be very much appreciated

Thanks
Suri

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 06-24-2010 at 02:37 PM.. Reason: Code tags
# 2  
Old 06-24-2010
Code:
[house@leonov] cat data.file
10,dravid,10000,5000,abc
20,sachine,20000,10000,bbc
30,raina,4000,1200,ccb
[house@leonov] V1='this'; V2='that'; sed -i data.file -e "s/^.*$/&,$V1,$V2/g"
[house@leonov] cat data.file
10,dravid,10000,5000,abc,this,that
20,sachine,20000,10000,bbc,this,that
30,raina,4000,1200,ccb,this,that

# 3  
Old 06-25-2010
MySQL

Thanks.
It is working fine.

Regards,
Suri.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add values to file in 2 new columns

Columns 4 and 5 are X and Y coordinates, column 6 is the elevation I would like to add 2 new columns at the end of the file with values the distance between first(X)(Y) and last location (X)(Y), based in 2 rows the difference in elevation = ($6-prev6) How to calculate the requested values... (6 Replies)
Discussion started by: jiam912
6 Replies

2. Shell Programming and Scripting

To add multiple columns in csv file

Hi Guys, Can you help to add static values at the end of the csv file with headers input_file id,name 1,neo 2,pull Expected id,name,status,entry,g_id 1,neo,done,2019-11-01T07:14:23,pass 2,pull,done,2019-11-01T07:14:23,pass My try but not able replacing properly and unable... (1 Reply)
Discussion started by: Master_Mind
1 Replies

3. UNIX for Dummies Questions & Answers

Help with solution to add together columns of large file

Hi everyone. I have a file with ~500 columns and I would like to perform a simple calculation on every two columns. The file looks like this: $cat input id A B C D E F.....X 1 2 4 2 3 4 1 n 2 4 6 4 6 4 5 n 3 4 7 5 2 2 3 n 4 ... (5 Replies)
Discussion started by: torchij
5 Replies

4. Shell Programming and Scripting

Add columns in the begin of the file

Hello Guys I need your kind help to solve the below issue I have an Flat file Format like CONCAT_KEY|FCTR_LVL_CD|RNWL_NS_CD|RTG_PLN_YR_CD 0000000000000010006100001001500005020071001|0|1|61 0000000000000010006100001003000010020071001|0|1|61 I want to add two column with default... (3 Replies)
Discussion started by: Pratik4891
3 Replies

5. UNIX for Dummies Questions & Answers

Add leading zeros to columns in a file

Hello Gurus, Quick question. I have a file with the following records: A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~ A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~ A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~ I wold like to do the following: 1. Add... (1 Reply)
Discussion started by: chumsky
1 Replies

6. Shell Programming and Scripting

Awk to add columns from a file into an existing file

Hi! I would need some help to add the last two columns of one file into another file using awk (or something similar). For example, I have: file 1: file 2: car book day root lag bar look pay boot tag tar took may moot sag I want to have:... (5 Replies)
Discussion started by: coconaza
5 Replies

7. Shell Programming and Scripting

Add columns in csv file

Hi everybody, I am new here and I need a reel help please A have a csv file and I want to add new in the end of the file Devisce Model,VMGuest Name,Host OS Name, Memory Size Value1,Value2,Value3,Value4 Value5,Value6,Value7,Value8 Value9,Value10,Value11,Value12 And I want to add to new... (3 Replies)
Discussion started by: villebonnais
3 Replies

8. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

9. Shell Programming and Scripting

add columns from file to another and sort

hi, i have probleme to extract the columns info from file to another one; FILE A look like : x,inof1 y,inof1 z,info2 t,inof3 and FILE B like x t w d z i want to add correpondant columns (info) to each line of FILE B and sort this file by this columns. (12 Replies)
Discussion started by: kamel.seg
12 Replies

10. Filesystems, Disks and Memory

manipulate csv file to add columns

Hi, I have a csv file with a key composed by 3 columns and some other numeric fields and I need to obtain the partial amounts by some part of the key. This may be some difficult to understand, so better see an example, where my input file is: name,surname,department,y2004,y2005,y2006... (6 Replies)
Discussion started by: oscarmon
6 Replies
Login or Register to Ask a Question