Home > Blockchain >  reading file and splitting the data from a file based on pattern in shell
reading file and splitting the data from a file based on pattern in shell

Time:08-02

I have a big file name lef_list with data in below format:

$PRJ_WORK_DIR/foundationip/tsmc/tsmc_lvt_stdcells/tcbn16ffcllbwp16p90cpdlvt/libs/ccs/tcbn16ffcllbwp16p90cpdlvtssgnp0p72vm40c_hm_ccs.lib.gz \
 $PRJ_WORK_DIR/mem/tech/TSMC_16FFC/v37/mem_gen/data_new/ram_dp_ulvt_pg_rd_64x44_mux4/NLDM/ram_dp_ulvt_pg_rd_64x44_mux4_ssgnp0p72vm40c.lib \

I want to read each line of file and split the lines into two based on pattern '/libs/' or '/NLDM/' in line and replace those with word '/LEF/' and write in a new file.

The output should be like :

$PRJ_WORK_DIR/foundationip/tsmc/tsmc_lvt_stdcells/tcbn16ffcllbwp16p90cpdlvt/LEF/ccs/tcbn16ffcllbwp16p90cpdlvtssgnp0p72vm40c_hm_ccs.lib.gz \
 $PRJ_WORK_DIR/mem/tech/TSMC_16FFC/v37/mem_gen/data_new/ram_dp_ulvt_pg_rd_64x44_mux4/LEF/ram_dp_ulvt_pg_rd_64x44_mux4_ssgnp0p72vm40c.lib \

I tried the below code:

while IFS="/libs/" read -r line val
do 
   echo "$line"/LEF/"$val"; 
done < lef_list

This code shows error as while expression syntax. command not found.

Someone please suggest any solution.

CodePudding user response:

A simple sed should do the job:

sed -E 's~/(libs|NLDM)/~/LEF/~g' file

$PRJ_WORK_DIR/foundationip/tsmc/tsmc_lvt_stdcells/tcbn16ffcllbwp16p90cpdlvt/LEF/ccs/tcbn16ffcllbwp16p90cpdlvtssgnp0p72vm40c_hm_ccs.lib.gz \
$PRJ_WORK_DIR/mem/tech/TSMC_16FFC/v37/mem_gen/data_new/ram_dp_ulvt_pg_rd_64x44_mux4/LEF/ram_dp_ulvt_pg_rd_64x44_mux4_ssgnp0p72vm40c.lib \

Pattern /(libs|NLDM)/ matches /libs/ or /NLDM/ and replaces that with /LEF/.

Or if you have to use awk only then:

awk '{gsub(/\/(libs|NLDM)\//, "/LEF/")} 1' file
  • Related