Prefix a variable in the first column of all the records of the files with and without header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prefix a variable in the first column of all the records of the files with and without header
# 1  
Old 10-24-2013
Prefix a variable in the first column of all the records of the files with and without header

In a bash shell, I have to prefix a variable to two .CSV files File1.CSV and File2.CSV. One of the files has a header and the other one is with no header in the below format:

Code:
"value11","value12","value13","value14","value15","value16"
"value21","value22","value23","value24","value25","value26"
"value31","value32","value33","value34","value35","value36"
"value41","value42","value43","value44","value45","value46"
"value51","value52","value53","value54","value55","value56"

I need to prefix each record with a variable which I derive from another file File3.CSV using some sed and awk commands. Lets say the variable is "string1 to string2" (the variable has double quotes already and the value keeps changing every week) from File3.CSV, now I have to append this to File1.CSV and File2.CSV in the first column of all the records as below.

Code:
"string1 to string2","value11","value12","value13","value14","value15","value16"
"string1 to string2","value21","value22","value23","value24","value25","value26"
"string1 to string2","value31","value32","value33","value34","value35","value36"
"string1 to string2","value41","value42","value43","value44","value45","value46"
"string1 to string2","value51","value52","value53","value54","value55","value56"

Please note, the variable value "string1 to string2" could be just a string, (or) from and to dates, (or) from and to numbers with spaces in between.

I tried to capture the string in a variable by name VAR1 and tried to call it in a sed and prefix it to the files, but did not work.

How could I achieve this for the file with the header and for the file without the header using either sed or awk

Last edited by dhruuv369; 10-24-2013 at 03:21 PM.. Reason: Adding the keywords/tags
# 2  
Old 10-24-2013
Try:
Code:
VAR1='"string1 to string2"'

Code:
sed "s/^/$VAR1,/" file

or
Code:
awk -v v1="$VAR1" '{print v1,$0}' FS=, OFS=, file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-24-2013
Try:
Code:
nawk 'NR==FNR{x=$0;next}{$0=x","$0}1' File3.CSV File1.CSV File2.CSV

# 4  
Old 10-24-2013
@Scrutinizer: The awk code works perfectly, however, the sed code gives me an error as below

Code:
sed: -e expression #1, char 11: unknown option to `s'

@bartus11: could you please help me understand your code? Thanks...
# 5  
Old 10-24-2013
Try
Code:
cat File1.CSV | sed 's/^/"string1 to string2",/'


Last edited by Scrutinizer; 10-24-2013 at 04:45 PM.. Reason: icode tags to code tags
# 6  
Old 10-24-2013
@Soham: I do not know what the "string1 to string2" will be every week. This keeps changing, and this is the main reason why I was trying to achieve it by using a variable.
# 7  
Old 10-24-2013
Try the following

Code:
var=\"something\"
cat File1.CSV | sed 's/^/$var/'

Every month you will have to change the value of var

Last edited by Scrutinizer; 10-24-2013 at 04:46 PM.. Reason: code tags
This User Gave Thanks to Soham 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

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. UNIX for Beginners Questions & Answers

Concatenate column values when header is Matching from multiple files

there can be n number of columns but the number of columns and header name will remain same in all 3 files. Files are tab Delimited. a.txt Name 9/1 9/2 X 1 7 y 2 8 z 3 9 a 4 10 b 5 11 c 6 12 b.xt Name 9/1 9/2 X 13 19 y 14 20 z 15 21 a 16 22 b 17 23 c 18 24 c.txt Name 9/1 9/2... (14 Replies)
Discussion started by: Nina2910
14 Replies

3. Shell Programming and Scripting

I want to find the difference between two files, only for the header (column names)

Hi All, I want to find the difference between two files, by checking only the headers (column names) and report if any new column is added in the latest file. For Ex: If the file "declartion.txt has these columns url;image;id;showcase_id;showcase_name and the actual file "feed.txt" has... (34 Replies)
Discussion started by: Praveen Pandit
34 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Specific Header after every 30 records

Hi All, I have got a requirement. I have a source file, EMPFULL.txt and I need to split the data for every 30 records and place a Typical Header as below with system and page number too. 2012.01.03 Employee Dept Report 1... (6 Replies)
Discussion started by: srk409
6 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. Shell Programming and Scripting

Extract date from file header and prefix it to all lines

Hello All, I have a file in the following format. I want to extract the date(020090930, 020090929) in the string "STPAGE020090930" and "STPAGE020090929" and prefix it to all lines below them. The output must be put into a new file. STPAGE020090930 xyzz aalc... (3 Replies)
Discussion started by: john2022
3 Replies

9. Shell Programming and Scripting

Merge text files while combining the multiple header/trailer records into one each.

Situation: Our system currently executes a job (COBOL Program) that generates an interface file to be sent to one of our vendors. Because this system processes information for over 100,000 employees/retirees (and growing), we'd like to multi-thread the job into processing-groups in order to... (4 Replies)
Discussion started by: oordonez
4 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question