File creation using awk and gsub command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File creation using awk and gsub command
# 1  
Old 03-18-2016
Computer File creation using awk and gsub command

I have input file
Code:
04000912|100:[[40.0,44]|[50.0,55]|[60.0,66]|[70.0,77]|[80.0,88]|[90.0,99]]|101:[[30.0,33]|[20.0,22]|[10.0,11]]|creDate:1451876825000|1441324800000:[[40.0]]|1444003200000:[[40.0]]|1446595200000:[[40.0]]|1449187200000:[[40.0]]|1451865600000:[[40.0]]

I have to get output as below
Code:
ID|Re_Date|Re_Value|Re_date
04000912|100|40.0|44
04000912|100|50.0|55
04000912|100|60.0|66
04000912|100|70.0|77
04000912|100|80.0|88
04000912|100|90.0|99
04000912|101|30.0|33
04000912|101|20.0|22
04000912|101|10.0|11
04000912|1441324800000|40.0
04000912|1444003200000|40.0
04000912|1446595200000|40.0
04000912|1449187200000|40.0
04000912|1451865600000|40.0

but iam getting the result as below
Code:
ID|Re_Date|Re_Value|Re_date
04000912|100|40.0|44
04000912|50.0|55
04000912|60.0|66
04000912|70.0|77
04000912|80.0|88
04000912|90.0|99
04000912|101|30.0|33
04000912|20.0|22
04000912|10.0|11
04000912|1441324800000|40.0
04000912|1444003200000|40.0
04000912|1446595200000|40.0
04000912|1449187200000|40.0
04000912|1451865600000|40.0


please found below command

Code:
awk 'BEGIN {print"ID|Re_Date|Re_Value|Re_date"}
{n=split($0,a,"|");
for(x=1;x<n+1;x++)
{ if(a[x]~/\[/)
{gsub(/:/,"|",a[x]);
gsub(/[][]/,"",a[x]);
gsub(/,/,"|",a[x]);
print a[1]"|"a[x]}}}
' test.txt >> sample.txt

could you please help me in getting the data in correct format

Last edited by Scott; 03-18-2016 at 09:49 AM.. Reason: Replaced ICODE tags with CODE tags
# 2  
Old 03-18-2016
Please use code tags as required by forum rules!

Try
Code:
awk '
BEGIN           {print"ID|Re_Date|Re_Value|Re_date"
                }

                {gsub (/[][]/, "")
                 for (i=2; i<=NF; i++)
                        {if ($i ~ /creDate/) continue
                         if (1 < split ($i, T, ":"))    {TMP = T[1]
                                                         $i = T[2] 
                                                        }          
                         gsub (/,/, "|", $i)
                         print $1, TMP, $i  

                        }
                }
' FS="|" OFS="|" file
ID|Re_Date|Re_Value|Re_date
0400057912|100|40.0|44
0400057912|100|50.0|55
0400057912|100|60.0|66
0400057912|100|70.0|77
0400057912|100|80.0|88
0400057912|100|90.0|99
0400057912|101|30.0|33
0400057912|101|20.0|22
0400057912|101|10.0|11
0400057912|1441324800000|40.0
0400057912|1444003200000|40.0
0400057912|1446595200000|40.0
0400057912|1449187200000|40.0
0400057912|1451865600000|40.0

# 3  
Old 03-18-2016
Hi Rudic,

Thanks for your reply.
if my input file don't have credate.for example
Code:
04000912|100:[[40.0,44]|[50.0,55]|[60.0,66]|[70.0,77]|[80.0,88]|[90.0,99]]|101:[[30.0,33]|[20.0,22]|[10.0,11]]|1441324800000:[[40.0]]|1444003200000:[[40.0]]|1446595200000:[[40.0]]|1449187200000:[[40.0]]|1451865600000:[[40.0]]

then what would be the code.
# 4  
Old 03-18-2016
You must be kidding. This is what you posted as an example:
Code:
04000912|100:[[40.0,44]|[50.0,55]|[60.0,66]|[70.0,77]|[80.0,88]|[90.0,99]]|101:[[30.0,33]|[20.0,22]|[10.0,11]]|creDate:1451876825000|1441324800000:[[40.0]]|1444003200000:[[40.0]]|1446595200000:[[40.0]]|1449187200000:[[40.0]]|1451865600000:[[40.0]]

What happens if you use the code on your new data?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-18-2016
Hi Rudic,

in some records we will have credate and in some records it will change.the code which you have provided it works fine.

for other records which is not having credate ,I have placed
if ($i ~ /[a-z]/) continue .


Thank you so much Rudic for your help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

2. UNIX for Dummies Questions & Answers

awk gsub with variables

Hello, I'm trying to substitute a string with leading zero for all the records except the trailer record using awk command and with variables. The input file test_med1.txt has data like below 1234ABC...........................9200............LF... (2 Replies)
Discussion started by: somu_june
2 Replies

3. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

4. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. OS X (Apple)

How to get the file creation date with find command

Is it possible to find all files based on the date of creation? And if so, how? I've been looking at the find command but it seems that only modification times are used as an option. (1 Reply)
Discussion started by: Straitsfan
1 Replies

6. Shell Programming and Scripting

awk gsub with variables?

Hey, I would like to replace a string by a new one. Teh problem is that both strings should be variables to be flexible, because I am having a lot of files (with the same structure, but in different folders) for i in daysim_* do cd $i/5/ folder=`pwd |awk '{print $1}'` awk '{ if... (3 Replies)
Discussion started by: ergy1983
3 Replies

7. Shell Programming and Scripting

Help with awk and gsub using C shell

Being new to awk, I am still running into little stupid things. For this issues I am trying to search for all occurrences of a string in a file and replace all of those occurrences with a replacement string. I tried doing awk '{gsub("|750101|", "|000000|", $0)}' infile > outfile Unix... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Shell Programming and Scripting

Awk Gsub Query

Hi, Can some one please explain the following line please throw some light on the ones marked in red awk '{print $9}' ${FTP_LOG} | awk -v start=${START_DATE} 'BEGIN { FS = "." } { old_line1=$0; gsub(/\-/,""); if ( $3 >= start ) print old_line1 }' | awk -v end=${END_DATE} 'BEGIN { FS="." } {... (3 Replies)
Discussion started by: crosairs
3 Replies

9. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

10. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies
Login or Register to Ask a Question