Adding field to file and moving the last 2 fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding field to file and moving the last 2 fields
# 1  
Old 01-25-2010
Adding field to file and moving the last 2 fields

I have a file with 32 fields each separated by ‘|”. I need to add a file date exactly in the format “ "20100120" “ as the 32nd field moving the existing 32nd field to 33. so the field I added should be 32nd and the 33rd field is the last field before I added the file date.

I know we can use sed and awk to replace or add for a file either at beginning or the end but not in the last but one position. Is there a way to do this?
# 2  
Old 01-25-2010
Code:
sed "s/|\([^|]*\)$/|20100120|\1/" file

Code:
awk -F"|" -v OFS="|" ' { $(NF+1)=$NF; $(NF-1)="20100120" ; print } ' file

# 3  
Old 01-25-2010
Thanks Anbu. But if have to add the file date from a variable like

file_DATE=$(date +'%Y%m%d')

how should this be re-written
# 4  
Old 01-25-2010
Code:
file_DATE=$(date +'%Y%m%d')
sed "s/|\([^|]*\)$/|$file_DATE|\1/" file

Code:
file_DATE=$(date +'%Y%m%d')
awk -F"|" -v OFS="|" -v dt=$file_DATE ' { $(NF+1)=$NF; $(NF-1)=dt ; print } ' file

Code:
file_DATE=$(date +'%Y%m%d')
awk -F"|" -v OFS="|" ' { $(NF+1)=$NF; $(NF-1)="'"$file_DATE"'" ; print } ' file

# 5  
Old 01-25-2010
thanks Anbu. Its working fine. if i need to have quotations around the file date like "20100125" how can i do that?
# 6  
Old 01-25-2010
Code:
sed "s/|\([^|]*\)$/|\"$file_DATE\"|\1/" file

Code:
awk -F"|" -v OFS="|" -v dt=$file_DATE -v qt='"' ' { $(NF+1)=$NF; $(NF-1)=qt dt qt ; print } ' file

# 7  
Old 01-26-2010
anbu i dont understand where you have mentioned the field 32, as i have the almost same issue but i need to change field18 and total numbers of fileds are 54.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding field to file

Hi I have file as given below 040|14300|40.0|563000 042|13200000|40.0 041|100|40.0 043|10000|40.0 045|102|40.0 I want to check if field 4 is null then I have to place | in position 4. result set should be 040|14300|40.0|563000 042|13200000|40.0| 041|100|40.0| 043|10000|40.0|... (9 Replies)
Discussion started by: shabeena
9 Replies

2. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

3. Shell Programming and Scripting

Adding Fields to the file

Hi All, I get a file on weekly basis from client. I need to write a script which make sure the file should have 20 columns after the first column. If not then the script should add the remaining columns and default them to space(except for 2nd and 3rd). and at the same time the script should... (25 Replies)
Discussion started by: Arun Mishra
25 Replies

4. Shell Programming and Scripting

Adding fields to a file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas TX|A0010998|West|US... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

5. Shell Programming and Scripting

Adding fields to file

Hi All, I have a file(Pipe Delimited) where i need to add a blank field before the last field and a blank field after the last field. Please help. I have provided below the sample input records and desired output. Code: Sample Input: A0010000|Abilene TX A 1|A0010957|Dallas... (0 Replies)
Discussion started by: Arun Mishra
0 Replies

6. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

7. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

8. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

9. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies

10. Shell Programming and Scripting

Moving Part of a field to another field using AWK

Hi there, I have a comma seperated file with nine fields the fields are rerate: "numberTX",field2,field3,field4,field5..... I want to do this to the file reate: "field5TX",field2,field3,field4,field5 I know I can do this using AWK, but the thing giving me fits is that I... (5 Replies)
Discussion started by: rjsha1
5 Replies
Login or Register to Ask a Question