I need some suggestion to make the below bash script using sed/awk in a one-liner way or if i can use python instead would be great for doing this search and replace task.
Here I created the search pattern in a file named "input.txt" with first column to search and second column for replace. Then I assign each column value on an array and calling in a for loop using sed utility to search and replace on "file.csv". This change only happens on the 3rd column only.
file.csv
Symbol,Name,Value
*,yy03LN-1,LM-GA-G01
*,yy5310-4,YP-QL-A03
*,yy5310-5,YP-QL-A10
input.txt
LM-GA-G01,LM-GA-G1
YP-QL-A03,YP-QL-A3
YP-QL-A10,YP-QL-A10
Expected results, to remove the 3rd column '0' number
file.csv
Symbol,Name,Value
*,yy03LN-1,LM-GA-G1
*,yy5310-4,YP-QL-A3
*,yy5310-5,YP-QL-A10
I created a shell script based on this condition, and this works better, but i need help similiar a single liner or short script to achieve this task.
#!/bin/bash
post=$(cat file.csv|awk -F "," '{print $NF}'| grep -v Index)
postar=($post)
for (( i=0; i<${#postar[@]}; ++i )); do
grep "${postar[$i]}" input.txt >> filtered.txt
done
left=$(cat filtered.txt|awk -F "," '{print $1}')
leftar=($left)
right=$(cat filtered.txt|awk -F "," '{print $2}')
rightar=($right)
for (( i=0; i<${#leftar[@]}; ++i )); do
sed -i -e 's/'"${leftar[$i]}"'/'"${rightar[$i]}"'/g' file.csv
done
Please note:- the line count of input.txt and file.csv are not same.
Please suggest
Thanks Jay
input.txtfile? It seems you just want to remove the zero-padding from integers...input.txtis not mandatory, i just placed to get the search/replace pattern. Not from every integers only string containsA01,``` B03,C02``` and not no action forA10,B20.