Redirect output of 'for' loop in cygwin

0

I have a directory that has 3 files : a.txt b.txt c.txt I have to delete first 4 lines from each file I want to use 'sed' or 'awk' or etc in Cygwin I do this:

for x in `ls`; do
    sed '1,4d' $x
done

It works fine BUT I need to have the output in a newly created files e.g. a2.txt b2.txt c2.txt. How could I refactor my loop? Thanks

susik

Posted 2016-06-08T14:37:00.950

Reputation: 447

Answers

0

Suppose you are using bash as your shell in Cygwin (that is by default), you can use the feature of bash parameter expansion as following:

for x in *.txt; do
    sed '1,4d' $x > ${x%.txt}2.txt
done

The key is ${x%.txt} to remove the suffix of file name (.txt) then append "2" and ".txt" as your example.

There are other refactoring: Using "ls" to get the list of file to be edited is redundant, using the path name expansion of shell itself is enough.

Yingyu YOU

Posted 2016-06-08T14:37:00.950

Reputation: 199

1

for x in *; do
    sed '1,4d' $x > $(basename $x .txt)2.txt
done

or

for x in *; do
    sed '1,4d' $x > ${x/./2.}
done

Michael Vehrs

Posted 2016-06-08T14:37:00.950

Reputation: 255