简体   繁体   English

每行运行命令?

[英]Run command for every line?

I have a bunch of 1255-138279.trans.txt files that contain lines like these: 我有一堆包含这些行的1255-138279.trans.txt文件:

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

For each line, I want to create a new file, named 1255-138279-0008.txt (or whatever the code at the start of the line is), and containing the remainder of that line (in this case TWO THREE ). 对于每一行,我想创建一个新的文件,命名为1255-138279-0008.txt (或任何在该行开始代码),以及包含该行的其余部分(在这种情况下, TWO THREE )。

This simple while loop in shell would do the job: Shell中的以下简单while循环可以完成这项工作:

while read -r id line; do
   echo "$line" >> "$id.txt"
done < file

Try this with : 尝试一下:

$ awk '{v=$1; $1=""; print $0 > "path/to/dir/"v".txt"}' file.txt
$ ls -1 path/to/dir/*.txt

I prefer doing this in two steps. 我更喜欢分两个步骤进行操作。 First, use sed or awk to generate a shell script: 首先,使用sedawk生成shell脚本:

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

Or with a POSIX-compliant regex (without GNU extensions): 或使用符合POSIX的正则表达式(无GNU扩展名):

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

Or using ERE: 或使用ERE:

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

Output: 输出:

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

Then execute that script to generate the files. 然后执行该脚本以生成文件。

. output.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用命令行替换文本文件中的每个前缀? - How to replace every prefix in a text file with command line? 从命令行在Java程序中传递运行参数会产生错误 - Passing run arguments in a java program from command line gives an error 在文件的每一行上运行curl命令并从结果中获取数据 - Run curl command on each line of a file and fetch data from result 如何在空行上下文中运行sublime-text命令? - How to run a sublime-text command in an empty line context? 使用什么 Vim 命令删除文件每一行某个字符后的所有文本? - What Vim command to use to delete all text after a certain character on every line of a file? 如何通过命令行告诉 Jest 不要运行某个测试文件,而是运行所有其他文件? - How can I tell Jest through the command line not to run a certain test file, but run all others? 删除该单词的每一行 - Delete Every line with that Word 匹配每一行的元素 - Match an element of every line 为什么我的正则表达式可以在 regexr.com 上运行,但从命令行运行时会抛出错误? - Why does my regex work on regexr.com but throws an errorwhen run from command line? 仅当代码在命令行中使用-n标志运行时才出现Unicode错误 - Unicode error only when code run with -n flag at the command-line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM