문제

I am trying to add two lines of text from one file to the beginning of a bunch of other files by using a for loop in the bash command line.

I tried this in the command line:

$for i in *.txt; do head -2 ../../041_R_def_c.txt > X; cat i >> X; \mv X i; done

but it gives me the error:

cat: i: No such file or directory

I also tried to just move one line to the beginning of the files:

$for i in *.txt; do echo "#c wavelength d %4.0f angstrom"|cat - i > /tmp/out && mv /tmp/out i; done
cat: i: No such file or directory
cat: i: No such file or directory
cat: i: No such file or directory

I thought that i would carry the name of the .txt file, but I don't think that is what i is. How can I get i to carry the name of the file/play the role of the file?

도움이 되었습니까?

해결책

In your loop, i is a variable, you need to access the contents of the variable with $, so it should be cat $i >> X. Otherwise, it thinks you want to cat the file named i, which doesn't exist. $i gives you the name stored inside the variable i.

You will run into the same issue with mv... You will want \mv X $i

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top