I was able to get my columns in csv format as shown below:
,col1,col2,col3,col4,col5,,
I used this awk command to get the ourput in this format:
awk -vORS=, '$0 && p {print $2}; $2 == "name" {p=1} '`
and then I used the following two commands to remove the leading and trailing two commas:
cols=${cols:1}
cols=${cols:0:${#cols}-2}
Now I get the output in this format:
col1,col2,col3,col4,col5
I want to remove specific columns from the right that match a list. For example if I call the function with parameter "col4,col5", awk should remove the last two columns and print the output like this:
col1,col2,col3
How can this be done in shell script(preferably with awk or grep or some other such shell supported command)?
Update: Initial file contents are output in a table as shown below:
+-----------------------------------------+--------+---------+
| name | type | comment |
+-----------------------------------------+--------+---------+
| col1 | int | |
| col2 | int | |
| col3 | string | |
| col4 | string | |
| col5 | string | |
+-----------------------------------------+--------+---------+
awk -F\|
to transform from pipe-separated into CSV.grep col /tmp/foo.txt | cut -d"|" -f2 | xargs | tr " " ","