0

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?

1
  • 3
    You should at least put an example of the kind of file that you're trying to parse. Commented Oct 23, 2012 at 12:02

2 Answers 2

5

Since you haven't specified about the field separator, I am assuming as the default(space) field separator.

awk '{print $4,$5>"/tmp/another_directory/"FILENAME}' *
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.