Today, i will discuss about the “How to remove extra delimiters in a file” using shell script. Below is the content of the file.
Post execution of shell script, the output will be
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.
#Reading character by character using while Loop.
while IFS= read -r -N 1 char
#Checking the existence of delimiter.
if [ “$char” = “|” ]
if [ $i = 1 ]
echo “Delimiter Arrived”
#If character is other than delimiter, then assign a value zero to variable i.
if [ “$char” != “|” ]
#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’ ]
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)
# Just printing(or appending) the characters.
echo “$char” >> “Path of the file”/text1.txt
done < "$INPUT"