Anamdev
Anamdev

Reputation: 43

convert column values to row after particular match in linux

I have a file in which values are all in 1 column in repetitive mode (1 set of values comes after say 10 row) . now i want to put this set of values from column to row (set wise) in repetitive mode. Showing some example as below

A = 1  
B = 2
C = 3
-----
A = 4
B = 5
C = 6

here i want output like below

1,2,3
4,5,6

Can anyone please help me in solving this?

Upvotes: 1

Views: 247

Answers (2)

NeronLeVelu
NeronLeVelu

Reputation: 10039

Line of same serie with separator (like in sample)

sed '/^---/ b treat
     s/[[:blank:]]\{1,\}//g;H;$!d
:treat
     s/.*//;x
     s/\n[^=]*=/,/g;s/,//
     ' YourFile

Assuming:

  • there is only digit as value
  • there is always a correct serie length (not feed with empty value is pe: B= is missing)

Upvotes: 0

Wintermute
Wintermute

Reputation: 44023

I'd say

awk '/^-----/ { print line; line = ""; sep = ""; next } { line = line sep $3; sep = "," } END { print line }' filename

This works as follows:

/^-----/ {               # If the current line is a delimiter
  print line             # print the stuff we just constructed (see below)
  line = ""              # reset state variables
  sep = ""
  next                   # do nothing else
}
{                        # otherwise:
  line = line sep $3     # append 3rd field of the current line to the output
  sep = ","              # <-- the first time around, sep is "", so there's no
                         #     comma before the first field in the output.
}
END {                    # When the input ends:
  print line             # print the last record.
}

Upvotes: 2

Related Questions