Reputation: 1
I would like to extract the data from the delimited values having different delimiter. For example :
file1.txt
head|first|java programming|hello|world
First level delimeter is | Second level delimiter is space.
awk -F"|" '{print $2," ",$3}'
This gives first java programming.
I would like to have printed only
first java
i.e. my second value $3, i have to delimit again. Note it is space in above example, could also be semi-colon.
i don't want to use pipe as this would be part of bigger awk script block.
Upvotes: 0
Views: 264
Reputation: 203995
$ awk -F'|' '{split($3,a,/[ ;]/); print $2, a[1]}' file
first java
Upvotes: 0
Reputation: 163457
If the delimiter can be either a space or a semicolon, you could use a character class -F"[ ;]"
Then for each part that you have, you can split on a pipe char, and print the 2nd and 3rd item.
Data
$cat file1.txt
head|first|java programming|hello|world
head|first|java;programming|hello|world
Example
awk -F"[ ;]" '
{
split($1,a,"|")
print a[2],a[3]
}
' file1.txt
Output
first java
first java
Upvotes: 0
Reputation: 247022
Use either pipe or space as the field separator:
awk -F '[ |]' '{print $2, $3}'
The default output field separator is space, so print $2, " ", $3
will print three spaces between the words.
Upvotes: 1