What is the easiest way to replace a column in a file?
basically, my file.txt
has 3 columns separated by ,
.
How can I change the second column with a bash script?
SveUJW24ibppfePgYeYHz7fC0,64BzZdqrYY7Tx8sbj5tmEW,yL6mCP0Do28k4EoTZUfKfqNYiIhGxxkA
xyRG8Da6kY35xeIT492Lul7xu,gTdmvjmahIOoyzmrttVMvTc1ER0bt,ne6RIM2TeMQAax1GgzL7FeDrnQyHPH1i
sxTf13KlAnjtXodJouQ9V6m5b,LzLtoEg18E1brm66dPjcHZfpI107nn4h,GUnApYwwDCZxWGZtzKzTU6sJRgHlUUfQ
7cjW5DZlXw1LYzVugbVyqfxRX,i7B4Q9w8h5anmMW87DfIBEm0AuNjbLGq,XttE1In9eZQ8puJVUriuNvx2AJAxviGf
XiLE8r9AMqy5YZQ9BbIS6m559,ToT2wbQdpNNySPxP1Tgz1,DssiszVBa05pbVDSOXNRaFXRxw0eZKHf
Sygrl5287BViOn0uQ9uCYipB1,TEYnXl6APWGbm9ckLCcHFUJzk7qS8JXH,sD2O46sbh1yVIluoyn6Zm2OKXYe05vV9
Qi6DxJ96M0hxNe4cgux3iJ1aS,LK3GHTpuo9kbmK9McRN4sFRQTGh2DU8J,wk2eF3f9xk5HowLzDIL3hCCNSmx8Uwi8
ZIX7qp5IIPekA0kzBdFR4IUQZ,9m9lEjfiotQ97s3uVN8EEP7Y1JmpgAk7,99ilfJWoJEBsKOfYI3buFfher07OCz6Y
Update
replace with another string in a variable. let's say var=new-sting
.
Actually, I was thinking I could do something like this:
sed "s/,[^,]*/,$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)/" file.txt
But it is not working as expected. I am having the same string over and over.
Best Answer
Assuming that
file.txt
contains lines of text that are divided into three columns with commas and that there are no additional commas anywhere, so each line has exactly two of them:Output:
This will process all lines at once and replace the middle column with the same value each time. The changed content will be printed to the terminal by default, if you want to modify
file.txt
in place instead, writesed -i
instead of plainsed
.If you need to update the replacement variable for each line anyhow (here a new random string for each line), you can loop over the lines like this:
Example output:
It's probably the easiest way to put this code snippet into a script file and then you ran redirect its output to a separate new file (not the original file from which you read!) like this: