18

I have a file str.txt with the following sample records.

31,2713810299,1,11-Aug-15 19:52:10
32,2713810833,1,11-Aug-15 21:36:18

Now I want to print output with awk as below.

cat str.txt|awk -F, '{print substr("$4",1,9)}' -

The output should be:

'11-Aug-15' 
'11-Aug-15' 
Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
user3548033
  • 593
  • 2
  • 12
  • 25

4 Answers4

30

a single quote would be \x27

awk -F, '{print "\x27"substr($4,1,9)"\x27" }'
FelixJN
  • 12,616
  • 2
  • 27
  • 48
3

POSIXly:

awk -F'[ ,]' -v q="'" '{print q$4q}' <file
cuonglm
  • 150,973
  • 38
  • 327
  • 406
1

A slightly different approach, for POSIX Awk:

awk -F'[, ]' '{printf "\047%s\047\n", $4}' file
'11-Aug-15'
'11-Aug-15'
jasonwryan
  • 71,734
  • 34
  • 193
  • 226
0

You could use awk command like this:

cat str.txt|awk -F, '{print substr($4,1,9)}' -

To get following output:

11-Aug-15
11-Aug-15

If single quotes requires at the start and end of lines then:

cat str.txt|awk -F, '{printf "\047%s\047\n",substr($4,1,9)}' -

To get following output:

'11-Aug-15'
'11-Aug-15'
snoop
  • 324
  • 2
  • 14