0

I have received a task to do these commands on a text file:

  1. Choose each second field from the file
  2. Sort and choose first 3 unique values
  3. Print the contents of each value.text while redirecting stderr

The thing I've come with is

cut cities.txt -f1 | sort| uniq | head -3 | xargs rename "s/*/*.text/" * | xargs cat 2>error.txt

but this gives an error:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE / at (user-supplied code).  

I also tried to do it with mv * *.text, but it doesn't work, saying mv: target 'Germany' is not a directory, with "Germany" being the last value in the list of values.

Either of these commands is fine with me, but explaining why both of them do not work (and how to make them work) would be more appreciated.

  • Replace rename "s/*/*.text/" with rename 's/.*/$&.txt/' – John1024 Sep 23 '17 at 19:23
  • We can't help you parse a file you're not showing us. Please edit your question and show us a a few representative lines of your file, and the output you expect from it. Also explain how fields are defined (spaces? tabs? commas? something else?). – terdon Sep 23 '17 at 19:25
  • Well, in case it isn't obvious, I should specify it: original file includes country-city names and I need to take the country names, the delimiter doesn't matter - my code finds the correct values, then sort and take first 3 unique values, which outputs as "Austria France Germany" with each word on a new line. Then I should try to print the content of Austria.text, France.text and Germany.text – Alex F Sep 23 '17 at 19:35
  • AlexF, if you want to get a good answer to your question, don't merely "describe" your file. Do what @terdon suggested and include sample lines in your question. Do this by clicking on edit. – John1024 Sep 23 '17 at 19:46
  • No, it isn't obvious. And your code makes no sense so it doesn't help us understand. Please edit as I asked so that we'll be able to help you. – terdon Sep 23 '17 at 19:48
1

In case anyone ever stumbles upon this question: John1024's comment to the question is the correct solution, though there's one thing to notice - the error messages generated by the attempt to print out the contents of non-existing files are generated by xargs rename "s/.*/$&.text/", therefore if you need to redirect your error messages to a different file, you have to append 2>error.text to the rename command, and not write another one | xargs cat 2>error.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.