Tengo un montón de archivos 1255-138279.trans.txt que contienen líneas como estas:

1255-138279-0006 THE SHAPE WENT SLOWLY ALONG BUT WITHOUT MUCH EXERTION FOR THE SNOW THOUGH SUDDEN WAS NOT AS YET MORE THAN TWO INCHES DEEP
1255-138279-0007 AT THIS TIME SOME WORDS WERE SPOKEN ALOUD ONE
1255-138279-0008 TWO THREE
1255-138279-0009 FOUR FIVE
1255-138279-0010 BETWEEN EACH UTTERANCE THE LITTLE SHAPE ADVANCED ABOUT HALF A DOZEN YARDS
1255-138279-0011 HERE THE SPOT STOPPED AND DWINDLED SMALLER
1255-138279-0012 THEN A MORSEL OF SNOW FLEW ACROSS THE RIVER TOWARDS THE FIFTH WINDOW
1255-138279-0013 THE RIVER WOULD HAVE BEEN SEEN BY DAY TO BE OF THAT DEEP SMOOTH SORT WHICH RACES MIDDLE AND SIDES WITH THE SAME GLIDING PRECISION ANY IRREGULARITIES OF SPEED BEING IMMEDIATELY CORRECTED BY A SMALL WHIRLPOOL
1255-138279-0014 THE WINDOW WAS STRUCK AGAIN IN THE SAME MANNER
1255-138279-0015 THEN A NOISE WAS HEARD APPARENTLY PRODUCED BY THE OPENING OF THE WINDOW
1255-138279-0016 SAID THE BLURRED SPOT IN THE SNOW TREMULOUSLY
1255-138279-0017 I ASKED WHICH WAS YOUR WINDOW FORGIVE ME
1255-138279-0018 WELL I SAID THAT YOU MIGHT
1255-138279-0019 O MUST I IT IS WHEN SHALL WE BE MARRIED FRANK
1255-138279-0020 I HAVE MONEY
1255-138279-0021 AND WE LIVE IN TWO PARISHES DO WE WHAT THEN
1255-138279-0022 IF I SAID SO OF COURSE I WILL
1255-138279-0023 THE FACT IS I FORGOT TO ASK
1255-138279-0024 GOOD NIGHT FRANK GOOD NIGHT

Para cada línea, quiero crear un nuevo archivo, llamado 1255-138279-0008.txt (o cualquiera que sea el código al comienzo de la línea) y que contenga el resto de esa línea (en este caso, TWO THREE).

1
theonlygusti 22 oct. 2017 a las 20:22

3 respuestas

La mejor respuesta

Pruebe esto con :

$ awk '{v=$1; $1=""; print $0 > "path/to/dir/"v".txt"}' file.txt
$ ls -1 path/to/dir/*.txt
1
Gilles Quenot 23 oct. 2017 a las 13:41

Prefiero hacer esto en dos pasos. Primero, use sed o awk para generar un script de shell:

sed "s/^\s*\(\S\+\)\s\+\(.*\)$/echo '\2' > '\1.txt'/g" input.txt > output.sh

O con una expresión regular compatible con POSIX (sin extensiones GNU):

sed "s/^[[:space:]]*\([^[:space:]]\+\)[[:space:]]\+\(.*\)$/echo '\2' > '\1.txt'/g" input.txt > output.sh

O usando ERE:

sed -E "s/^[[:space:]]*([^[:space:]]+)[[:space:]]+(.*)$/echo '\2' > '\1.txt'/g" input.txt > output.sh

Salida:

...
echo 'TWO THREE' > '1255-138279-0008.txt'
echo 'FOUR FIVE' > '1255-138279-0009.txt'
...

Luego ejecute ese script para generar los archivos.

. output.sh
1
Ruud Helderman 25 oct. 2017 a las 11:13

Este simple bucle while en shell haría el trabajo:

while read -r id line; do
   echo "$line" >> "$id.txt"
done < file
2
anubhava 22 oct. 2017 a las 18:47