You are not logged in.

#1 2023-12-12 15:02:50

tethys
Member
Registered: 2019-08-13
Posts: 91

[SOLVED] What is wrong with this syntax?

I want to sort a file except first two lines with the following command:

(head -n 2 <file> && tail -n +3 <file> | sort) > newfile

but I get the following error message: "bash: syntax error near unexpected token `&&'".

Disclaimer: I found the command after an internet search, I am not sure if it is correct, but first I would like to understand why I get the above error message.

Last edited by tethys (2023-12-13 07:01:07)

Offline

#2 2023-12-12 15:19:29

Stefan Husmann
Member
From: Germany
Registered: 2007-08-07
Posts: 1,391

Re: [SOLVED] What is wrong with this syntax?

AFAICS the && should be a |

Offline

#3 2023-12-12 16:23:10

eda2z
Member
From: Woodstock, IL
Registered: 2015-04-21
Posts: 66

Re: [SOLVED] What is wrong with this syntax?

I tried the command substituting .bashrc for <file> and it worked. Did you leave in the < and > characters? If I leave in the > before the && then I get the same error.

Offline

#4 2023-12-12 18:21:11

tethys
Member
Registered: 2019-08-13
Posts: 91

Re: [SOLVED] What is wrong with this syntax?

OK, I understood now why bash spits the error. As I said, I took the expression from here, and as I know stackoverflow as a serious site, I thought that I do not understand the expression. But the expression is wrong: one cannot redirect into a pipe, ie one cannot have something like "> |"! Everything is OK if one throws away BOTH ">" inside the subshell ().

Both following expressions work as expected:

(head -n 2 <file && tail -n +3 <file | sort) > newfile
(head -n 2 <file; tail -n +3 <file | sort) > newfile

Offline

#5 2023-12-12 18:25:57

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,310

Re: [SOLVED] What is wrong with this syntax?

the expressions isn't wrong <file> is often used as a placeholder, it does not mean '<' then the file name then '>', it just means the file name. If you had posted the actual command you used, it would have been glaringly obvious; never paraphrase, always tell us what you actually did.

It is wrong in the way that Stefan Husmann pointed out, though.

Online

#6 2023-12-12 18:48:57

tethys
Member
Registered: 2019-08-13
Posts: 91

Re: [SOLVED] What is wrong with this syntax?

Thanks Scimmia for pointing this out. Just now I realized the same.

Offline

#7 2023-12-12 20:39:07

seth
Member
Registered: 2012-09-03
Posts: 48,398

Re: [SOLVED] What is wrong with this syntax?

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Ftr:

Scimmia wrote:

It is wrong in the way that Stefan Husmann pointed out, though.

Afaiu the OP tethys wants to keep the first two lines where they are, sort the rest and write all of that into a file

(seq 100 101; seq 1 10 | shuf) | (head -n2 </dev/stdin; tail -n+3 </dev/stdin | sort -n)

A pipe between head and tail would result in empty output, because the head output is only two lines that get then skipped by the tail.

Offline

#8 2023-12-12 23:35:29

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,310

Re: [SOLVED] What is wrong with this syntax?

seth wrote:

Ftr:

Scimmia wrote:

It is wrong in the way that Stefan Husmann pointed out, though.

Afaiu the OP tethys wants to keep the first two lines where they are, sort the rest and write all of that into a file

(seq 100 101; seq 1 10 | shuf) | (head -n2 </dev/stdin; tail -n+3 </dev/stdin | sort -n)

A pipe between head and tail would result in empty output, because the head output is only two lines that get then skipped by the tail.

Ah, right, that's what I get for not reading the first post closely enough.

Online

Board footer

Powered by FluxBB