Ignore the first line

3

I am using the following command to organize a file:

sort -u arquivo.csv -o arquivo.csv

But I need to ignore the first line of it, in this case the header.

How can I do this?

    
asked by anonymous 05.06.2018 / 19:15

2 answers

2

Use the tail command with the -n option:

tail -n +2 arquivo.csv

In this case, -n +2 will display only from the second line on (you can change the number to any value you need). The + sign is also important because if you only type -n 2 , it will show the last two lines.

Then you can pass the result directly to sort :

tail -n +2 arquivo.csv | sort -u > arquivo_ordenado.csv

In addition, I recommend saving the output to another file (as I did above), since reading and writing to the same file does not usually work very well (from experience, depending on the command, the file is overwritten and / or truncated, and you end up losing the data - I do not know how sort handles this, but I'd rather not risk it.)

The Lacobus answer answers this specific question about sort .

Keep the header

As well remembered in the comments, with the command above, the first line (header) is lost. If you need it in the final file, one way to do this is to break in two commands.

First save the first line in the final file, with the command head :

head -1 arquivo.csv > arquivo_ordenado.csv

The parameter -1 tells you to get only the first line. Then do tail and sort , and add the result to the file:

tail -n +2 arquivo.csv | sort -u >> arquivo_ordenado.csv

Notice that I am now using >> , which adds the information at the end of the file. If I use only > , the contents of the file is overwritten and the header will be lost.

    
05.06.2018 / 19:28
3

You can use the tail utility to bypass the first line of the input file before redirecting its output to sort , see:

$ tail -n +2 arquivo.csv | sort -u -o arquivo.csv

According to the documentation of the sort utility, the ordering process is done in memory, before opening / writing the output file (specified with the -o option), this ensures an in of the file securely.

Notice that the first line of the file is destroyed with the execution of this command.

Reference: link

    
06.06.2018 / 15:53