If your grep
support -o
option, you can try:
$ grep -o '[[:digit:]]*' file | paste -sd+ - | bc
784
POSIXly:
$ printf %d\\n "$(( $(tr -cs 0-9 '[\n*]' <file | paste -sd+ -) ))"
784
TAB delimiter
cut
You do not need sed
or awk
, a simple cut
will do:
cut -f2 infile
awk
If you want to use awk, the way to supply the delimiter is either through the -F
argument or as a FS=
postfix:
awk -F '\t' '{ print $2 }' infile
Or:
awk '{ print $2 }' FS='\t' infile
Output in all cases:
"Here's the field of the text, also contains comma"
"Here's the field of the text, also contains comma"
"Here's the field of the text, also contains comma"
"Here's the field of the text, also contains comma"
Quote delimiter
If the double-quotes in the file are consistent, i.e. no embedded double-quotes in fields, you could use them as the delimiter and avoid having them in the output, e.g.:
cut
cut -d\" -f4 infile
awk
awk -F\" '{ print $4 }' infile
Output in both cases:
Here's the field of the text, also contains comma
Here's the field of the text, also contains comma
Here's the field of the text, also contains comma
Here's the field of the text, also contains comma
Best Answer
It's more easily done with
perl
: