Reputation: 93
In Unix, suppose a file contains 5 fields & data such as:
"112233"|"Roshan"|"25"|" FAX 022 3987789 \| TEL 77766288892 \| abc "|"Male"
need to extract 4th field. using below
column_value=`echo $line | cut -f4 -d'|'
This only gives us - " FAX 022 3987789 \
but need " FAX 022 3987789 \| TEL 77766288892 \| abc "
as 4th column value.
Effective delimiter should be -
"|"
Upvotes: 0
Views: 1907
Reputation: 85600
cut
is not the right tool for the job when it involves a multi-character de-limiter needed for parsing input string/file.
You can use GNU Awk with FPAT which defines how each field in a record should look like. You can write FPAT
as a regular expression constant in which case something like below should work.
FPAT = "(\"[^\"]+\")"
Using this in the Awk
command,
line='"112233"|"Roshan"|"25"|" FAX 022 3987789 \| TEL 77766288892 \| abc "|"Male"'
awk '
BEGIN {
FPAT = "(\"[^\"]+\")"
}{print $4}' <<<"$line"
produces an output as
" FAX 022 3987789 \| TEL 77766288892 \| abc "
Regular Expression - Test results
Upvotes: 1
Reputation: 6426
you can add the two extra fields as follows
echo $line | cut -f 4,5,6 -d\|
alternatively you could use sed to replace the "|" delimiter with a different char (for example a tab)
echo $line | sed s/\"\|\"/\t/g | cut -f 4
Upvotes: 0