Add columns in the begin of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add columns in the begin of the file
# 1  
Old 09-26-2011
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
Code:
 
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 values and the o/p should be
Code:
 
 
DATA_ACQ_CYC_CNTL_ID|DATA_ACQ_CYC_CNTL_SNPSHT_DT|CONCAT_KEY|FCTR_LVL_CD|RNWL_NS_CD|RTG_PLN_YR_CD
1783579|090511|0000000000000010006100001001500005020071001|0|1|61
1783579|090511|0000000000000010006100001003000010020071001|0|1|61

So I want to add column DATA_ACQ_CYC_CNTL_ID and DATA_ACQ_CYC_CNTL_SNPSHT_DT at the begining of file with default value here those are 1783579 and 090511 but it can change
# 2  
Old 09-26-2011
Code:
sed '
1   s/^/DATA_ACQ_CYC_CNTL_ID|DATA_ACQ_CYC_CNTL_SNPSHT_DT|/
2,$ s/^/1783579|090511|/
' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 3  
Old 09-27-2011
Hi Yazu
Thanks a lot for your reply

But the change is reflecting for first line
The header is coming and one record .The second record is missing.

May be its because the original file has two records so when its appending the header its truncating the last line

Is there any workaround for this

Thanks again for finding time to help me
# 4  
Old 09-27-2011
Code:
awk '{print (NR>1)?"1783579|090511|" $0:"DATA_ACQ_CYC_CNTL_ID|DATA_ACQ_CYC_CNTL_SNPSHT_DT|" $0}' infile

This User Gave Thanks to rdcwayx For This Post:
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. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

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: 10,dravid,10000,5000,abc 20,sachine,20000,10000,bbc 30,raina,4000,1200,ccb ... (2 Replies)
Discussion started by: onesuri
2 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