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.
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:
I'd suggest an approach based on the perl Bytes::Random::Secure module, based on Filling up column of text file with random data using bash modified to use your desired mix of upper and lower case letters and decimal digits:
Alternatively, if you want to use your
/dev/urandom
pipeline, one way to do that without external looping would be using a FIFO with awk'sgetline
function:make the FIFO
$ mkfifo _fifo
execute your command, streaming its output to the FIFO
or (eliminating the useless use of cat)
get lines from the FIFO and substitute them in to the lines of the target file
remove the FIFO
Testing:
With GNU awk, you can do the equivalent internally using
getline
with a co-process:Using
awk
:-v
: create a variable namedvar
which contains the string "mystring"-F,
: use,
as field separatorBEGIN {OFS = FS}
set output field separator equal to field separator to keep the separator (comma) after replacement{$2 = var; print}
replace the field 2 (column 2) withvar
content; then print.You can also change
-v var="mystring"
with something like-v var="$variable"
which$variable
is an variable in your environment.Here is an example:
let's run the command: