Reputation: 43
I have some text where the delimiter column count/order is not same line to line.
Line 1 A=123, B=456, C=123, A=456, D=1234
Line 2 B=123, A=456
Line 3 A=123, A=789
I want to print only the As and Ds within each line, what is the best way to do this?
So the output should be,
123 456 1234
456
123 789
Upvotes: 0
Views: 257
Reputation: 58401
This might work for you (GNU sed):
sed -rn '/\<[AD]=/{s//\n/g;s/[^\n]*\n([0-9]*)[^\n]*/ \1/g;s/^ //p}' file
If a line contains an A
or a D
variable replace the said variable name by a newline and then extract the following digit sequence placing a space in front as a separator. Finally remove the first space and print out.
Upvotes: 0
Reputation: 17188
Pure Bash. Remove "x=...," where x is not A or B:
shopt -s extglob
while read line ; do
line="${line//[^AD]=+([0-9])?(, )/}"
line="${line//[AD=,]/}"
echo "$line"
done < "$infile"
Output.
123 456 1234
456
123 789
Upvotes: 1
Reputation: 203493
$ awk -F'[ =,]+' '{
ofs=""
for (i=3;i<NF;i+=2) {
if ($i~/[AD]/) {
printf "%s%s",ofs,$(i+1)
ofs=OFS
}
}
print ""
}' file
123 456 1234
456
123 789
Upvotes: 2
Reputation: 33327
Alternative way, with awk:
$ awk -F'[ =,]+' '{for (i=1; i<NF;i+=2) if ($i=="A" || $i =="D")printf "%s ", $(i+1);print ""}' file
123 456 1234
456
123 789
Upvotes: 0
Reputation:
Try this.
BEGIN { FS=", " }
{
for (i = 1; i <= NF; i++) {
split($i, parts, "=")
if (parts[1] == "A" || parts[1] == "D") {
printf("%s ", parts[2])
}
}
print ""
}
The input:
$ cat in.txt
A=123, B=456, C=123, A=456, D=1234
B=123, A=456
A=123, A=789
Usage:
$ gawk -f s.awk in.txt
123 456 1234
456
123 789
Upvotes: 1