Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

awk to capture element upto space or using special escape character

Trying to extract the 5th element in $1 after the - upto the space or \\. If a / was used then the script awk -F'[-/'] 'NR==0{print; next} {print $0"\t""\t"$5}' file works as expected. Thank you :).

file –tab-delimited–

00-0000-L-F-Male    \\path\to   xxx xxx
00-0001-L-F-Female  \\path\to   xxx xxx

desired (last field has two tabs before)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

00-0000-L-F-Male    \\path\to   xxx xxx         Male
00-0001-L-F-Female  \\path\to   xxx xxx         Female

awk

awk -F'-[[:space:]][[:space:]]+' 'NR==0{print; next} {print $0"\t""\t"$5}' file

00-0000-L-F-Male        \\path\to       xxx     xxx
00-0001-L-F-Female      \\path\to       xxx     xxx

awk 2

awk -F'[-\\]' 'NR==0{print; next} {print $0"\t""\t"$5}' file

awk: fatal: Unmatched [ or [^: /[-\]/

>Solution :

Using any awk:

$ awk -F'[-\t]' -v OFS='\t\t' '{print $0, $5}' file
00-0000-L-F-Male    \\path\to   xxx xxx     Male
00-0001-L-F-Female  \\path\to   xxx xxx     Female

Regarding your scripts:

awk

awk -F'-[[:space:]][[:space:]]+' 'NR==0{print; next} {print $0"\t""\t"$5}' file

  1. -F'-[[:space:]][[:space:]]+' says that your fields are separated by a - followed by 2 or more spaces, which they aren’t.
  2. NR==0{foo} says "do foo for line number 0" but there is no line number 0 in any input.

awk 2

awk -F'[-\\]' 'NR==0{print; next} {print $0"\t""\t"$5}' file

  1. -F'[-\\]' appears to be trying to set FS to a minus sign or a backslash, but you already told us your fields are tab-separated, not backslash-separated.
  2. When setting FS this way it goes through a few different phases of interpretation, converting a shell string to an awk string and converting an awk string to a regexp, and using the regexp as a field separator, so you need several layers of escaping, not just 1. If unsure, keep adding backslashes will the warnings and errors go away.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading