I have a large flat file with variable length fields that are pipe delimited. The file has no new line or CR/LF characters to indicate a new record. I need to parse the file and after some number of fields, I need to insert a CR/LF to start the next record.
Input file
1|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t
If counting 4 fields, I need the output to be
1|b|c|d
e|f|g|h
i|j|k|l
m|n|o|p
q|r|s|t
Thank you for your assistance
#!/bin/bash
FIELDS=4
IFS="|"
QUIT=0
while [ "$QUIT" -eq 0 ]
do
ARR=()
for ((N=0; N<FIELDS; N++))
do
if ! read -d '|' ARR[$N]
then
QUIT=1
break;
fi
done
[ ! -z "${ARR[0]}" ] && echo "${ARR[*]}"
done < infile > outfile
Another script that does what you want:
#!/bin/bash
while read -d "|" j; do
s="$s$j|"; ((i++))
if ((i%4==0)); then
echo "${s%|}"
i=0; s=''
fi
done < 'yourdatafile'
echo "$s$j"
exit 0
Variables: $j read what's inside 2 `|`
$s is the string to display each four iterations
$i counts how many reads we did so far; it's reset every four loops to prevent overflow in case of maaaaany reads =)
'yourdatafile' is the file you want to parse. This script does not alter your file, it simply echoes on the stdout (your terminal). To redirect the content of stdout to another file, just call the script as follow:
./script > 'anotherfile'