I have a folder with 1000 text files. I would like to extract fourth and fifth columns from each file and save it to an another folder with the same filenames. How can I do this with awk or Bash?
-
3You should at least put an example of the kind of file that you're trying to parse.Goles– Goles2012-10-23 12:02:45 +00:00Commented Oct 23, 2012 at 12:02
Add a comment
|
2 Answers
With GNU find and GNU sed, assuming space separated columns and the files to process are in the current directory:
mkdir tmp/
find . -maxdepth 1 -type f -print0 |
xargs -0 -l /bin/sh -c 'sed -r "s/([^ ] *){3}([^ ]) *([^ ]*).*/\2 \3/" "$1" > "tmp/$1"' ''
Note the last pair of single quotes are important, they're a placeholders so the filename gets passed correctly to sh. The processed files are saved to tmp/ in the current directory.