How can I make my script simple?

Hi .. I am trying to print first row few columns and last row few column...

I am doing like this... I want to do using single awk

for file in *.xyz; do
dt_end=$(awk 'END{print $2 "\t" $3 "\t" $4}' FS="," $file)
dt_start=$(awk 'FNR == 1{print $1 " \t"$2 }' FS="," $file )
echo $dt_start $dt_end
done

Try sth like this..

 
awk 'FNR==1{printf $1 " \t"$2" "}
FNR==1 && NR>1 {print s} 
{s=$2"\t"$3"\t"$4}' FS="," *.xyz
awk -F, 'FNR==1{print $1,$2}END{print $2,$3,$4}' OFS=, *.xyz
suppose if I want print dt_end in 
awk 'FNR == 1{print $1 " \t"$2 } FS="," file

How to include variable dt_end in above

---------- Post updated at 05:53 AM ---------- Previous update was at 05:41 AM ----------

Out put not coming properly...

need like this

dt_start \t dt_end

Be careful to run this only on one single file at a time. If you run it on multiple files, there'd be many starts and ends that will be more difficult to separate. Try this:

$ tmp=$(awk -F, 'NR==1{print $1,$2}  END{print $2,$3,$4}'  OFS=,  ORS=" "  A.xyz)
$ dt_start=${tmp%% *}
$ dt_end=${tmp#* }
$ echo "$dt_start      $dt_end"
40454,31    51358,834,743 

In your 1st posting in this thread, you wanted the output to be

L1F1 \tL1F2 LLF2\tLLF3\tLLF4\n

for each input file where L1F1 is the contents of Line 1 Field 1, L1F2 is Line 1 Field 2, LLF2 is Last Line Field 2, LLF3 is Last Line Field 3, and LLF4 is Last Line Field 4.

Here you are saying the output you want is:

L1F1 \tL1F2 \t LLF2\tLLF3\tLLF4\n

for each input file.

If I'm reading the code supplied by others correctly, pamu's code will give you the information you asked for for every file except the last file given, but the 1st two fields of the 1st two files will be printed before the last fields of the 1st file and subsequent lines (until the last file) will have the 1st fields from file x and the last fields from file x-1. The fields from the last line of the last file will not be printed. In addition, if L1F1 or L1F2 in any file contains a % character or a \ character, the results might not be what you requested.

Subbeh's code will only print the 1st line data from the 1st file and the last line data from the last file.

And, RudiC's code will print:

L1F1,L1F2      LLF2,LLF3,LLF4\n

as long as there are no spaces in any of the fields being printed, but will only process one file at a time.

Note also that the standards do not define the values of $2, $3, or $4 in an END action (but I don't know of any implementation of awk that doesn't do what you want). The code pamu provided is the only one that handles this portably.

I believe the following script does what you requested (as modified above) portably:

awk -F, '
FNR == 1 && NR > 1 {    print s}
FNR == 1 {              printf("%s \t%s \t ", $1, $2)}
{                       s = $2 "\t" $3 "\t" $4}
END {                   print s}' *.xyz

as long as you /usr/xpg4/bin/awk or nawk rather than awk if you are running on a Solaris/SunOS system.

Ouch, I missed the separating <TAB>s! And, I concentrated to get those fields into the variables dt_start and dt_end, assuming the requestor wanted to process them further. FAILED by far!