0

I am trying to use awk, split and then print but running into error

Input:

id  day userId  itemId

1   12/1/17 8:32    2232    tv

2   1/12/18 10:18   3232    fdfs

3   2/9/18 10:50    232     fdsf

4   3/6/18 12:35    345456  fdg

Expected output

id  datetime    monthyear   time    userId  itemId

1   12/1/17 8:32    12/17   8:32    2232    tv

2   1/12/18 10:18   1/18    10:18   3232    fdfs

3   2/9/18 10:50    2/18    10:50   232     fdsf

4   3/6/18 12:35    3/18    12:35   345456  fdg
  • Hello and welcome to this site! Care to share the exact command(s) you're trying and what error you're getting? – Mr Shunz Sep 14 '18 at 15:41
  • What is the field separator in your input file? – glenn jackman Sep 14 '18 at 15:56
  • Command used awk -F",", '{OFS=","; split($2,a,"/"); $5=a[1]"/"a[3]; print}' book1.csv >> testbook.csv – thinkingsavvy Sep 14 '18 at 16:26
  • field separator is , – thinkingsavvy Sep 14 '18 at 16:29
  • error I am getting is awk: illegal statement at source line 1 sometimes awk: bailing out at source line 1 or awk: no program given – thinkingsavvy Sep 14 '18 at 16:33
1

How about

awk -F"\t" '
NR == 1         {$2 = "datetime" OFS "monthyear" OFS "time"
                }
NR > 1          {split ($2, T, "[/ ]")
                 $2 = $2 OFS T[1] "/" T[3] OFS T[4]
                }
1
' OFS="\t" file
id  datetime    monthyear   time    userId  itemId
1   12/1/17 8:32    12/17   8:32    2232    tv
2   1/12/18 10:18   1/18    10:18   3232    fdfs
3   2/9/18 10:50    2/18    10:50   232 fdsf
4   3/6/18 12:35    3/18    12:35   345456  fdg
  • I run into error awk: syntax error at source line 2 context is NR == 1 {$2 = >>> ? <<< awk: illegal statement at source line 2 – thinkingsavvy Sep 14 '18 at 16:51
  • What's your OS and awk versions? You may want to try nawk. – RudiC Sep 14 '18 at 17:07
  • I am using Mac OS, awk works and I have this code which works to some extent in order to get MM/YYYY however it writes into a new line rather than a new column (I am testing by writing entire content to a new file) awk -F"," '{OFS=FS}{split($2,a,"[/ ]"); $7=a[1]"/"a[3]; print}' book1.csv > testbook.csv – thinkingsavvy Sep 14 '18 at 17:47
  • It worked after I slightly modified the code as below, however in csv the MonthYear was formatted incorrectly. ex: Instead of 12/17 it was 17-Dec (12/17/2018) awk -F "," 'NR == 1 {$2 = "DayTime" OFS "MonthYear" OFS "Time"}{ if (NR>1){split($2,a,"[/ ]");$2=$2;$5=$3;$6=$4;$3=a[1]"/"a[3];$4=a[4]}}1' OFS="," Test.csv > Test1.csv – thinkingsavvy Sep 17 '18 at 10:33
0

RudiC shows how to transform the file. To save it back into the same file:

  1. using GNU awk:

    gawk -i inplace '...' file
    
  2. using sponge from the moreutils package:

    awk '...' file | sponge file    
    
  3. Or using a temp file

    tmp=$(mktemp)
    awk '...' file > "$tmp" && mv "$tmp" file
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.