LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "for" loop with awk (https://www.linuxquestions.org/questions/linux-newbie-8/for-loop-with-awk-636324/)

michael_nc 04-18-2008 04:51 PM

"for" loop with awk
 
Hi, I am new to linux and awk, and am trying to run a script that will execute an awk command that deletes the first column of a file for every file in a directory.

I think it should look like this:

for i in *; do awk '{$1=""}1' $i > $i_done.txt; done

When I run this it finished, but I don't get any new output files, only one file called ".txt" which appears to have all the results in it.

But when take the command out of the for loop and just run it on one file, it works exactly as it should.

I am using Cygwin on a windows box.

Any help appreciated.

Thanks,

Mike

colucix 04-18-2008 04:57 PM

You're using a variable which is not assigned:
Code:

> $i_done.txt
this uses a variable called "i_done". It should be
Code:

> ${i}_done.txt
note that the dot is not included in the variable name (and so the following text) whereas the underscore is.

Tinkster 04-18-2008 05:12 PM

Hi,
And welcome to LQ!

And a sed alternative that saves you the loop...

Code:

sed -r -iori 's/^[^ \t]+[^ \t](.*)/\1/' *
Admittedly the regex may look a tad daunting ;}


Cheers,
Tink


All times are GMT -5. The time now is 07:16 PM.