I'd like to process many *.txt files in a directory - they have a common structure (; separated, common headers) but vary in the number of lines each is comprised of i.e. some are just a single line, others up to 8 lines long..I would like to delete any lines other than the first and the last line for each file. Any helpful pointers? Update: I've included some test data files as requested:stat87.txtStations_id; Stationshoehe; Geogr.Breite; Geogr.Laenge; von_datum; bis_datum; Stationsname;                                                                                                              87;     ; 46.1123;  8.5440;19010101;19661229;Dresstat01.txt                                                                                                                                                   Stations_id; Stationshoehe; Geogr.Breite; Geogr.Laenge; von_datum; bis_datum; Stationsname;                                                                                                              1;     ; 47.8400;  8.8500;18910101;19580228;Aach                                                                                                                                                    1;  478; 47.8413;  8.8493;19580301;19860630;Aachand stat56.txt for example.Stations_id; Stationshoehe; Geogr.Breite; Geogr.Laenge; von_datum; bis_datum; Stationsname;                                                                                                              56;     ; 46.4580;  7.6320;18980101;19450321;Hamb 56;     ; 46.4580;  7.6320;19450321;19880511;Hamb                                                                                                                                                    56;  103; 46.4411;  7.6345;19880601;19990630;HambIn this case I'd be especially looking to keep the first line of columns 5 and the last of column 6, so that the time-span of the station is captured.Results:find . -type f -name \*.txt -printf %f\0 | xargs -0 -I xxxx sed -ni '2 {  $ {    s/^[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\);\([^;]*\).*$/\1;\2/    p    q  }  s/^[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\).*$/\1/  p}$ {  s/^[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\).*$/\1/  p}' xxxxgenerates....19010101;1966122918910101198606301898010119990630I then use a simple sed loop to clean up the final files, by adding the ;'for file in *.txt; do  sed 'N;s/\n/;/' $file > cleaned$file done'19010101;1966122918910101;1986063018980101;19990630
sed delete all besides first and last line of many files
text processing;sed
Perfect, there is below the awk only version:find . -type f -name \*.txt -printf %f\0 | xargs -0 -I xxxx sed -ni ' 2 {   $ {     s/^[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\);\([^;]*\).*$/\1;\2/     p     q   }   s/^[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\).*$/\1/   h } $ {   s/^[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\).*$/\1/   H   x   s/\n/;/   p }' xxxxThank you to the very famous Sed - An Introduction and Tutorial by Bruce BarnettResults:$ cat stat01.txt18910101;19860630$ cat stat56.txt18980101;19990630$ cat stat87.txt19010101;19661229----first version for referenceBased on your input, I invented data file format and a sed script to work them.Give this a try:$ find . -type f -name \*.txt -printf %f\0 | xargs -0 -I xxxx sed -ni ' 2 {   $ {     s/^[^;]*;\([^;]*\);\([^;]*\).*$/\1;\2/     p     q   }   s/^[^;]*;\([^;]*\).*$/\1/   p } $ {   s/^[^;]*;[^;]*;\([^;]*\).*$/\1/   p }' xxxxIt removes the first line which contains the headers.It keeps only column 2 of the first data line encountered and the column 3 of the last data line of the file.If a file contains only one data line, it keeps on one line column 2 and column 3.hehehe this is odd, but I had a lot of fun !!!The data files in the current directory:$ cat test01.txtName;Price;Amount;DescriptionBread;2.1;3;healthy one$ cat test02.txtName;Price;Amount;DescriptionWater;0.0;100;For lifeWine;10.3;1;Less than half a glass a day$ cat test03.txtName;Price;Amount;DescriptionHouse;1000.0;1;homeCar;500.5;0;no needBike;10.3;5;Good for the planet and for meResults:$ cat test01.txt2.1;3$ cat test02.txt0.01$ cat test03.txt1000.05Please provide 2 short data files content and the expected result, and I will modify this answer.