HungryFerret
HungryFerret

Reputation: 43

Printing multiple fields of text between delimiters, sed or awk

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

Answers (5)

potong
potong

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

Fritz G. Mehner
Fritz G. Mehner

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

Ed Morton
Ed Morton

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

user000001
user000001

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

user1907906
user1907906

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

Related Questions