Skip to content
Advertisement

How to replace string with bash script and write back the Result

There is a CSV file with some columns, the first column is a 5 digit customer number, the other columns are separated with “;”

Here is a example:

12345;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
67890;some;other;cols;comes;here
34567;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
;some;other;cols;comes;here
24315;some;other;cols;comes;here

If the first column is empty than I need to set the last given customer ID. The result should look like:

12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
67890;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
24315;some;other;cols;comes;here

Now I read the file linewise with bash script, and want to check if the line starts with a number. If yes than explode the line by “;” and set customerID with array[0] (the first value). Next I check if the line not began with a number and want to write the five digits on the begin of the line. But I can’t access the Array Index with the customer ID.

This is my script:

#!/bin/bash
while read line
do
    row=$line
    if echo $row |grep "^[0-9].*$" > /dev/null;
      then
        arr=$(echo $row | tr ";" "n")
        echo ${arr[0]};
    fi
done < $1

I get the whole line without “;” and not the CustomerID as arr[0] next I don’t know how to write the digits at begin of the line back to the file. Anybody can help me?

Advertisement

Answer

Try:

awk -v id=12345 -F ';' '$1==""{$1=id;} {id=$1; print}'  OFS=';' file
  • awk uses field separator ; which makes you access each individual field as $1, $2, $3 etc.
  • -v id=12345 is a command line argument that you pass to awk to be used when first field is empty
  • $1="" is a condition that checks whether first field is empty
  • $1=id is setting $1 to passed variable id
  • {id=$1; print} sets the id variable to be used for next line and then prints the line

OUTPUT:

12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
12345;some;other;cols;comes;here
67890;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
34567;some;other;cols;comes;here
24315;some;other;cols;comes;here
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement