Adding an extra date column in UNIX file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding an extra date column in UNIX file
# 1  
Old 09-30-2018
Adding an extra date column in UNIX file

Hi All,

I have a file with only one column of data (without delimiter). For Ex:

Code:
cat temp.txt
22055
21088
93840
30990
50990
50950

I want to insert an additional column with current date as value. So, i have used below command but didn't get the result as excepted. Could onyone over here please help me in writing the current script command. Also i need to get rid of "space" after the comma delimiter.

Code:
awk -vd=`date +%Y%m%d` -F"," -OFS="," '{$2=d; print $1,$2}' temp.txt
22055, 20180928
21088, 20180928
93840, 20180928
30990, 20180928
50990, 20180928
50950, 20180928

# 2  
Old 09-30-2018
What is wrong with the result ("didn't get the result as expected)?

Code:
$ sed "s/$/,$(date '+%Y%m%d')/" file
22055,20180930
21088,20180930
93840,20180930
30990,20180930
50990,20180930
50950,20180930

Add the -i option to sed, if it's supported in your version, to write the changes directly to the file if the result is as expected.
This User Gave Thanks to Scott For This Post:
# 3  
Old 09-30-2018
The result you present is not quite reproducible. -OFS="," causes an error, but when corrected like -vOFS="," , it should yield the desired output without space. You could even simplify your code snippet like
Code:
awk -vd=`date +%Y%m%d` -F"," -vOFS="," '{print $1, d}'  file
22055,20180930
21088,20180930
93840,20180930
30990,20180930
50990,20180930
50950,20180930

This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-01-2018
Thanks both of you for your response and showing the error in the command which I return.


1. I clarified with sed command.
2. For the output with AWK command what is the difference between OFS and vOFS (should we need to use when we pass a variable to the awk script)?


Could you please let me know.


Thanks & regards,
Suresh G
# 5  
Old 10-01-2018
-v declares a variable to use inside awk. You could also put that at the end, without -v. e.g.
Code:
awk -vd=`date +%Y%m%d` -F"," '{print $1, d}' OFS="," file

# 6  
Old 10-01-2018
For 2.: Your phrasing doesn't target the right question. We're dealing with two things:

a) the -v option to awk, which you correctly deploy for the passing of the d variable. man awk:
Quote:
-v var=value assigns value to program variable var.

b) the redefinition of the OFS variable. This can be done in many ways, amongst which

  • -vOFS="<chars?>" in front of the script.
  • OFS="<chars?>" trailing the script
  • BEGIN {OFS="<chars?>"...} (or any other place) within the script
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding new column data in csv from UNIX

Adding new column data in csv from UNIX Hi I need to add new column data daily to existing csv file. Please assist 7/11 7/10 7/9 7/8 space 10 GB 20 GB I was able to generate current day's data in csv but unable to add the previous 30 days data to the same csv Please use code tags,... (2 Replies)
Discussion started by: archana25
2 Replies

2. Shell Programming and Scripting

Adding Extra Column in txt file base on Condition

HI Guys, I have below input. Output Base on Below Condition. 1> if forth column is empty and next coming line have same name with \es then add that column name on all rows 2>rest of all are es:vsDataEUtranCellFDD Input:- CCL01736 CCL01736_7A_1 es:vsDataEUtranCellFDD ... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

Need help in column comparison & adding extra line to files

Hi, I wanted to check whether the x,y,z coordinates of two files are equal or not. At times, when one file is converted to another suitable file extension , there are some chances that the data mismatch would happen during the conversion. In order to avoid the data misfit, i would like to... (6 Replies)
Discussion started by: b@l@ji
6 Replies

4. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

5. Shell Programming and Scripting

Adding extra word from file1 to file2

I need to add a word from file1 to file2 accordinggly... file1 contains name of servers and file2 version of server I need that information in a single file so that the format is server_name : version I been trying but havent been able to figure out how to search for a file using sed... ... (14 Replies)
Discussion started by: eponcedeleonc
14 Replies

6. Solaris

Adding Extra Hard Disk

Hi Solaris users - I have an Ultra10 SPARC machine, with IIe processor. To prepare for the Solaris10 admin exam PartII I need to set up the metadb/mirroring in my machine, but do not know how to do this properly. I need this to practice the mirroring tasks. If anyone could help it would be... (3 Replies)
Discussion started by: patcom
3 Replies

7. Shell Programming and Scripting

sort file adding extra character

HI all i have this script : #!/bin/bash sort /usr/tmp/"REPORT"$1 -o \ /usr/tmp/"SREPORT"$1 -k 1,7 -S 150 end of script now i'm doing this command : ls -lsgt *REPORT* 4 -rw-r--r-- 300 Sep 16 REPORT54784 4 -rw-r--r-- 301 Sep 16 SREPORT54784 as you can see the sorted file... (5 Replies)
Discussion started by: naamas03
5 Replies

8. UNIX for Dummies Questions & Answers

Adding a date as a first column

I want to add a date to a record which is appended to a file that gets its data from an external source. An explanation: 1. Getting external data: curl http://www.example.com/temperatures.txt 2. Getting the required record: | grep mylocation 3. Appending to file: >> mytemperatures.txt ... (2 Replies)
Discussion started by: figaro
2 Replies

9. IP Networking

Adding an extra route to the ip routing table

In my college dorm, there is a file sharing network in the entire building. Problem is, there is only a manual for windows with the settings on how to connect... :mad: They say that you have to give the following command in cmd in windows: route add 172.16.71.0 mask 255.255.255.0... (2 Replies)
Discussion started by: Japie89
2 Replies
Login or Register to Ask a Question