Using the bash
shell:
while IFS= read -r line; do
name=${line%%::*}
contents=${line#*::}
echo -e "$contents" >"$name"
done <fileA.txt
This reads the input file line by line and extracts the output filename and the contents form the read line using standard parameter substitutions. ${variable%%pattern}
removes the longest matching substring from the tail end of $variable
that matches pattern
, while ${variable#pattern}
removes the shortest matching substring from the start of $variable
that matches pattern
.
The value of line
is read with read -r
so that the backslashes are preserved in the data. Without -r
, read
would not preserve these. We also set IFS
to an empty string before calling read
so that no flanking whitespace is trimmed from the data.
The output is done using echo -e
, which interprets the escape sequences in the data. This means that it will replace the \n
in the data with actual newlines.
After running this, you will have
$ ls -l
total 12
-rw-r--r-- 1 kk wheel 71 Jun 17 16:28 fileA.txt
-rw-r--r-- 1 kk wheel 24 Jun 17 16:31 trans_fileA
-rw-r--r-- 1 kk wheel 16 Jun 17 16:31 trans_fileB
$ cat trans_fileA
abcd
dfghc
091873
hhjj
$ cat trans_fileB
a11d
11hc
73345
Related: