Combine Two Commands Output

How i can combine output of two commands in one file.......i tried this but it is not working although each command is working good seperately.....

head -1 filename | tail -1 filename

i think there is problem with command concatenator?

(head -1 filename; tail -1 filename )> newfilename

Hope you are trying to print the first and last line to a new file

Yes that is done.Thanks....wat if I want to insert some word in between the 2 commands but on same line?

head -1 filename; "Some Text" ; tail -1 filename > newfilename

it is taking "Some text" as command instead of text.......how to combine text now?

(head -1 filename; echo "Some Text" ; tail -1 filename )> newfilename

the second command after the echo starts in new line not on the same line.........

Use the -n option in echo if you want to display it in a single line.

echo -n 'Header is: '; echo -n head -1 file1.txt ; echo -n ' Trailer is: '; echo -n tail -1 file1.txt > new_file

Now the problem is that it is displaing all correct on the screen but when I open new_file, it just shows me this result:

tail -1 file1.txt > new_file

I want it to redirect all output in one line which it is showing on screen to new file..........

(echo -n 'Header is: '; head -1 file1.txt ; echo -n ' Trailer is: ';  tail -1 file1.txt )> new_file 

I tried this but with this command, only trailer is written on the new_file........i.e. only output of last command is there not of previous three commands.......

My bad, i have missed the grouping of the commands. So it should be

(echo -n 'Header is: '; head -1 file1.txt ; echo -n ' Trailer is: ';  tail -1 file1.txt) > new_file

Dear,

we r back to the problem.........it gives output in 2 lines now.......

Header 1 is: Header Value
Trailer 1 is: Trailer Value

where as i want like this:

Header 1 is : Header value Trailer 1 is: Trailer value

i.e. all 4 in same line.......I guess the command head -1 file1.txt starts new line after it finishes...........

and wen I execute this script:

(echo -n 'Header is: ' ; echo -n head -1 file1.txt ; echo -n ' Trailer is: ' ; echo -n tail -1 file1.txt) > file2

It gives all prints all in one line and does not execute the command of head and tail in this case.....output is :

Header is: head -1 file1.txt Trailer is: tail -1 file1.txt

Finally this should help you. In future it would be better if you tell the full requirement rather than breaking it up.

This is similar to this Header details

(echo -n 'Header is: ' ; head -1 file1.txt ; echo -n ' Trailer is: ' ;  tail -1 file1.txt) | tr -d '\n' > file2
awk ' { if ( NR == 1 ) { head=$0 } else { last = $0 } }END{ printf "Header is : %s Trailer is : %s\n", head, last }' filename

Thanks a lot..........it is now working fine with both ways....I shall post in future more requirements and sample output.....sorry for that..........let me check with 100MB file and see the performance in both cases.........

Is there any way that we can fix first 300 characters for header and then 300 characters for trailer no matter how long or short is the actual data.......i.e. if header/trailer length is less than 300 characters, it should append white spaces and if it is more than 300 characters, it should ignore the chacaters>299....................and all the things in the first line........

Then you can modify the awk command as

awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename

Output like

 Header is : Test                                                                                                      Trailer is : Tail                                                

I am not sure it will restrict the output to 300 chars. Check that out

thanks a lot......it is working perfect......now only one issue left of performance..............the situation was like this:

Header
Record 1
Record 2
.......
Record n
Tail

The above code awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename converted Header and Trailer in first line and within 300 character each.

Now I want to overwrite this one line above (Header+ Tail) over the first line of original file (orginal header) and delete the Tail........i can copy this header and tail to new file and then append Record 1 to Record n to this new file, but that will cause too low performance........so I think better to update first and the last line of the this file.................so how can i do that?

i.e. 1-update the header with new header+trailer
2-delete the tail in original file (may be using cut tail -1 filename?)