Reputation: 65
I am new at shell scripting and I am trying to read data from a txt file which is in the following format.
A: 1, 2, 3, 4
B: 1,2; 3, 4
Here is my code:
awk -F':/,/;/ ' '{ echo $2 $3 $4 $5 $5 }' -f 'testread.txt'
I just need the numbers from the text file. Thanks.
Upvotes: 2
Views: 3051
Reputation: 46816
The easiest way is probably just to make your field separator "anything that isn't numeric".
[ghoti@pc ~]$ cat input.txt
A: 1, 2, 3, 4
B: 1,2; 3, 4
[ghoti@pc ~]$ awk -F'[^0-9]+' '{$1=$1; print;}' input.txt
1 2 3 4
1 2 3 4
[ghoti@pc ~]$
The $1=$1
bit is just to force awk to rewrite $0
using the default output field separator.
Update:
More explicit output:
[ghoti@pc ~]$ awk -F'[^0-9]+' '{printf("1=%s 2=%s 3=%s 4=%s 5=%s\n", $1, $2, $3, $4, $5);}' input.txt
1= 2=1 3=2 4=3 5=4
1= 2=1 3=2 4=3 5=4
[ghoti@pc ~]$
Upvotes: 2
Reputation: 400
There are several issues with your command.
The argument for the -F
option is a regular expression that specifies your desired delimiters. To select colons, commas, or semicolons as your delimiters, you need to use pipes, not slashes. Alternatively, you could use a bracket expression.
The AWK statement to output text is print
, not echo
.
The -f
option is to specify a file containing an AWK program, not an input file. You don't need -f
to specify an input file.
You want something more like this:
awk -F '[:,;]' '{ print $2 $3 $4 $5 }' testread.txt
Upvotes: 2