Today, i will discuss about the “How to remove extra delimiters in a file” using shell script. Below is the content of the file.
a|b||c
d||e|||f
Post execution of shell script, the output will be
a|b|c
d|e|f
Below is the script which is created to remove extra delimiter in a file.
#file is assigned to variable INPUT . Here we used two files text1.txt and text2.txt to manipulate the source file data.
INPUT=”Path of the file”/file.txt
#Truncating text1.txt. Make sure this file exists.
>text1.txt
#Reading character by character using while Loop.
while IFS= read -r -N 1 char
do
echo “$char”
#Checking the existence of delimiter.
if [ “$char” = “|” ]
then
i=$((i+1))
if [ $i = 1 ]
then
echo “Delimiter Arrived”
fi
fi
#If character is other than delimiter, then assign a value zero to variable i.
if [ “$char” != “|” ]
then
i=0
#If a character a new line, then paste all characters in one line with delimiter pipe. This code changes as per the type of Delimiter.
if [ “$char” = $’\n’ ]
then
echo “New Line Found”
cat text1.txt |paste -sd “|” >> text2.txt
# As new line found, Delete all characters which are present in text1.txt file and go to Next Loop(Using continue Code)
>text1.txt
continue
fi
# Just printing(or appending) the characters.
echo “$char” >> “Path of the file”/text1.txt
fi
done < “$INPUT”