Home > OS >  Need to replace value in new line [duplicate]
Need to replace value in new line [duplicate]

Time:10-06

I have the data (test.txt) in the below format.

x: a
y: b

I need it in this format. (1st thing that comes before: is the header, after the space the value is data) (edited)

x,y  
a,b 

I can do like

cat test.txt | tr ": " '\n'

x
a

y
b

It just comes in the new line. Is there any way I can achieve the desired format?

CodePudding user response:

How about the BSD tool rs:

# Remove colons
<infile tr -d : |

# Transpose input
rs -T           |

# Change the delimiter
tr -s ' ' ,

This question and it's answers might provide some other alternatives.

CodePudding user response:

This might work for you (GNU sed):

sed -E 'sed -E 'N;s/:\s*(.*)\n(.*):\s*/,\2\n\1,/' file

Append the following line and substitute using pattern matching.

CodePudding user response:

1st solution: With your shown samples, please try following awk program.

awk '
BEGIN{ OFS="," }
{
  for(i=1;i<=NF;i  ){
    if(i==1){
      sub(/:$/,"",$i)
    }
    value[i]=(value[i]?value[i] OFS:"")$i
  }
}
END{
  for(i=1;i<=NF;i  ){
    print value[i]
  }
}
' Input_file

Explanation: Adding detailed explanation for above.

awk '                        ##Starting awk program from here.
BEGIN{ OFS="," }             ##Setting OFS to comma in BEGIN section.
{
  for(i=1;i<=NF;i  ){        ##Traversing through all fields here.
    if(i==1){                ##If its first field then do following.
      sub(/:$/,"",$i)        ##Substitute ending colon with NULL in 1st field.
    }
    value[i]=(value[i]?value[i] OFS:"")$i  ##Creating value with index of i and keep appending $i in it.
  }
}
END{                         ##Starting END block from here.
  for(i=1;i<=NF;i  ){        ##Traversing through all fields here.
    print value[i]           ##Printing value with index of i here.
  }
}
' Input_file                 ##Mentioning Input_file name here.

2nd solution: In case your Input_file can have dynamic(not fixed) number of fields per lines then try following, which will print values till maximum number of field numbers.

awk '
BEGIN{ OFS="," }
{
  for(i=1;i<=NF;i  ){
    if(i==1){
      sub(/:$/,"",$i)
    }
    value[i]=(value[i]?value[i] OFS:"")$i
  }
  nf=(nf>NF?nf:NF)
}
END{
  for(i=1;i<=nf;i  ){
    print value[i]
  }
}
' Input_file
  • Related