awk substitute variable value in nth field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk substitute variable value in nth field
# 1  
Old 04-14-2011
awk substitute variable value in nth field

I have a large csv file that looks like this:

Quote:
ID,user,date,text,flags,read
1,5001,1299911473,Working offsite,1,0
2,5002,1299949357,Working onsite, lwp applies,3,1
3,5003,1299952854,Working onsite,2,0
4,5004,1299953929,Working offsite, no lwp, supervised,1,1
The 3rd field is a unix time stamp that I want to convert to human readable.
I wrote a bash script with this code:

Code:
IFS=$','
cat $1  | while read ID user DATE text flags read; do
echo -e "$ID,$user,$(date -d @$DATE),$text,$flags,$read
done
unset IFS
exit 0

The problem is that the 4th field (text field) may or may not contain a comma, thus messing up my field separation, when it comes to outputting. The third field (when a comma is used as the FS) is ALWAYS the unix time stamp. I want to change change just the 3rd field in the file by substituting the unix time stamp for the human readable one (achieved with "date -d@DATE").

Any help much appreciated

stumpy
# 2  
Old 04-14-2011
Are you sure you want to use pure shell for that?
Code:
perl -F, -lane'
  $F[2] = localtime $F[2] if $. > 1;
  print join ",", @F
  ' infile

or:

Code:
perl -lpe'
  s{((?:\d+,){2})(\d+)}{$1.localtime $2}e
  ' infile


Last edited by radoulov; 04-14-2011 at 07:48 AM..
# 3  
Old 04-14-2011
Hi radoulov

Thanks very much for the help. I have no knowledge of perl, hence I went with bash. Your code was correct based on the information that I gave you.
I noticed some problems with the output on a large file that I processed with your code. It seems that there is new line characters in some of the 4th fields (text field) that mess up the output, I guess that is because a new line character acts as a Field Separator. Any thoughts on how to handle this. I am guessing that with newline characters in the text field I am sunk here?

I used your first suggestion (perl -F, -lane etc)
# 4  
Old 04-14-2011
Hm,
could you post a sample of the problematic input?
I need it to reproduce the issue.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

4. Shell Programming and Scripting

awk - printing nth field based on parameter

I have a need to print nth field based on the parameter passed. Suppose I have 3 fields in a file, passing 1 to the function should print 1st field and so on. I have attempted below function but this throws an error due to incorrect awk syntax. function calcmaxlen { FIELDMAXLEN=0 ... (5 Replies)
Discussion started by: krishmaths
5 Replies

5. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

6. Shell Programming and Scripting

Awk product of nth field

How do I do the product of nth filed just like sum. For sum I know like awk '{ sum += $12 } END {printf "%.2f\n", sum}' works as initial sum = 0. But for product how do initialize the variable to 1? (2 Replies)
Discussion started by: madhavb
2 Replies

7. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

8. Shell Programming and Scripting

AWK variable substitute issue

dear, I have below file called folderlist.txt # ParentFolder environment_flag SubFolders triss 1 checksum bookstructure 1 fx 1 checksum_GMDB I have a script which which will create the folders under... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

9. Shell Programming and Scripting

how to find the nth field value in delimiter file in unix using awk

Hi All, I wanted to find 200th field value in delimiter file using awk.? awk '{print $200}' inputfile I am getting error message :- awk: The field 200 must be in the range 0 to 199. The source line number is 1. The error context is {print >>> $200 <<< } using... (4 Replies)
Discussion started by: Jairaj
4 Replies

10. Shell Programming and Scripting

Need help to get the nth field of the variable

Below is the format of my file which consist of 1)BSB 2)BSB/ADS 3)CIB 4)CIB/CRH 5)CIB/DCC 6)CIB/EMD 7)CIB/GDSPresentation 8)CIB/HCH 9)CIB/HSM 10)CIB/MCH 11)CIB/RCH 12)COB 13)DCI 14)DIB 15)DIB/H2H 16)DIB/HotelSync 17)DIB/UADBA (11 Replies)
Discussion started by: rohit22hamirpur
11 Replies
Login or Register to Ask a Question