Reputation: 11
My input is test.txt which contains data in this format:
'X'=>'ABCDEF',
'X'=>'XYZ',
'X'=>'GHIJKLMN',
I want to get something like:
'ABCDEF',
'XYZ',
'GHIJKLMN',
How do I go about this in bash?
Thanks!
Upvotes: 1
Views: 56
Reputation: 21955
Using awk
awk 'BEGIN{FS="=>"}{print $2}' file
'ABCDEF',
'XYZ',
'GHIJKLMN',
FS
in awk stands for field separator. The code inside BEGIN
is executed only at the beginning, ie, before processing the first record. $2
prints the second field.
A more idiomatic way of putting the above stuff would be
awk 'BEGIN{FS="=>"}$2' file
'ABCDEF',
'XYZ',
'GHIJKLMN',
The default action in awk
is to print the record. Here we explicitly mention what to print. ie $2
.
Upvotes: 0
Reputation: 4455
Here's a solution using sed:
curl -sL https://git.io/fjeX4 | sed 's/^.*>//'
Sed is passed a single command: s///. is a regex that matches any characters (.*) from the beginning of the line (^) to the last '>'. The is an empty string, so essentially sed is just deleting all the characters on the line up to the last >. As with the other solutions, this solution assumes that there is only one '>' on the line.
Upvotes: 1
Reputation: 187994
If the data is really uniform, then you could just run cut (on example input):
$ curl -sL https://git.io/fjeX4 | cut -d '>' -f 2
'ABCDEF',
'XYZ',
'GHIJKLMN',
You can see flag explanations on explainshell.
With awk, it would look similar:
$ curl -sL https://git.io/fjeX4 | awk -F '>' '{ print $2 }'
'ABCDEF',
'XYZ',
'GHIJKLMN',
Upvotes: 0
Reputation: 241748
If the input never contains the character >
elsewhere than in the "fat arrow", you can use cut
:
cut -f2 -d\> file
-d
specifies the delimiter, here >
(backslash needed to prevent the shell from interpreting it as the redirection operator)-f
specifies which field to extractUpvotes: 1